From 7fb768f8889148ea5e38a1a5b5960287f22c9901 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 12:41:27 +0800 Subject: [PATCH 01/20] add enable_to_static and drop some methods of ProgramTranslator --- python/paddle/jit/__init__.py | 2 + python/paddle/jit/api.py | 1 + .../jit/dy2static/program_translator.py | 544 ++++++++++-------- 3 files changed, 297 insertions(+), 250 deletions(-) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index fd5ca115c2e2e7..2315a4c879540e 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 enable_to_static from .dy2static.logging_utils import set_code_level, set_verbosity from . import dy2static @@ -32,4 +33,5 @@ 'set_code_level', 'set_verbosity', 'not_to_static', + 'enable_to_static' ] diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index f55aeb5c1b8cc1..4372f712a6ad8a 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -48,6 +48,7 @@ ProgramTranslator, StaticFunction, unwrap_decorators, + enable_to_static, ) from paddle.jit.translated_layer import ( TranslatedLayer, diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index a01fb286c78226..2f45d004bb9cf0 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -1158,7 +1158,7 @@ def __init__(self): if self._initialized: return self._initialized = True - self._program_cache = ProgramCache() + # self._program_cache = ProgramCache() self.enable_to_static = True def enable(self, enable_to_static): @@ -1203,278 +1203,322 @@ def func(x): ) self.enable_to_static = enable_to_static - def get_output(self, dygraph_func, *args, **kwargs): - """ - Returns the output dygraph Tensor for dygraph function. The dygraph - function will be translated into static graph function so the under - beneath numerical result will be calculated by static graph mode. - - Args: - dygraph_func (callable): the dygraph function. - *args (tuple): the input argument of dygraph_func. - **kwargs (dict): the input argument of dygraph_func. - - Returns: - Tensor or tuple of Tensors: the dygraph Tensor containing digital result. - - Examples: - .. code-block:: python - - import paddle - - - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v - - - prog_trans = paddle.jit.ProgramTranslator() - - x = paddle.ones([1, 2]) - x_v = prog_trans.get_output(func, x) - print(x_v) # [[0. 0.]] - - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_output" - - if not self.enable_to_static: - # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) - # will show up **only once**. - logging_utils.warn( - "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. " - "We will just return dygraph output. " - "Please call ProgramTranslator.enable(True) if you would like to get static output." - ) - return dygraph_func(*args, **kwargs) - try: - function_spec = FunctionSpec(dygraph_func) - cache_key = CacheKey.from_func_and_args( - function_spec, - args, - kwargs, - getattr(dygraph_func, '__self__', None), - ) - _, partial_program_layer = self._program_cache[cache_key] - - if args and isinstance(args[0], layers.Layer): - # Synchronize self.training attribute. - partial_program_layer.training = args[0].training - args = args[1:] - try: - return partial_program_layer(args) - except BaseException as e: - # NOTE: - # 1. If e is raised in compile time, e should have been attached to ERROR_DATA before; - # 2. If e raised in runtime, e should be attached to ERROR_DATA here. - if not hasattr(e, error.ERROR_DATA): - # runtime error - error.attach_error_data(e, in_runtime=True) - raise - except BaseException as e: - error_data = getattr(e, error.ERROR_DATA, None) - if error_data: - error_data.raise_new_exception() - else: - logging_utils.warn( - "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'" - " if you can't handle this {} yourself.".format(type(e)) - ) - raise e - - def get_func(self, dygraph_func): - """ - Returns a callable function which converts imperative dygraph APIs of - the input dygraph_func into declarative net-building APIs, which means - it doesn't return immediate digital result as get_output does. - Users should handle Program and Executor by themselves. - - Args: - dygraph_func (callable): the dygraph function. - - Returns: - callable: converting imperative dygraph APIs into declarative - net-building APIs. - - Examples: - .. code-block:: python - - import paddle - - - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v - - - prog_trans = paddle.jit.ProgramTranslator() - static_func = prog_trans.get_func(func) - print(callable(static_func)) # True - - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_func" + # def get_output(self, dygraph_func, *args, **kwargs): + # """ + # Returns the output dygraph Tensor for dygraph function. The dygraph + # function will be translated into static graph function so the under + # beneath numerical result will be calculated by static graph mode. + + # Args: + # dygraph_func (callable): the dygraph function. + # *args (tuple): the input argument of dygraph_func. + # **kwargs (dict): the input argument of dygraph_func. + + # Returns: + # Tensor or tuple of Tensors: the dygraph Tensor containing digital result. + + # Examples: + # .. code-block:: python + + # import paddle + + + # def func(x): + # if paddle.mean(x) > 0: + # x_v = x - 1 + # else: + # x_v = x + 1 + # return x_v + + + # prog_trans = paddle.jit.ProgramTranslator() + + # x = paddle.ones([1, 2]) + # x_v = prog_trans.get_output(func, x) + # print(x_v) # [[0. 0.]] + + # """ + # assert callable( + # dygraph_func + # ), "Input dygraph_func is not a callable in ProgramTranslator.get_output" + + # if not self.enable_to_static: + # # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) + # # will show up **only once**. + # logging_utils.warn( + # "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. " + # "We will just return dygraph output. " + # "Please call ProgramTranslator.enable(True) if you would like to get static output." + # ) + # return dygraph_func(*args, **kwargs) + # try: + # function_spec = FunctionSpec(dygraph_func) + # cache_key = CacheKey.from_func_and_args( + # function_spec, + # args, + # kwargs, + # getattr(dygraph_func, '__self__', None), + # ) + # _, partial_program_layer = self._program_cache[cache_key] + + # if args and isinstance(args[0], layers.Layer): + # # Synchronize self.training attribute. + # partial_program_layer.training = args[0].training + # args = args[1:] + # try: + # return partial_program_layer(args) + # except BaseException as e: + # # NOTE: + # # 1. If e is raised in compile time, e should have been attached to ERROR_DATA before; + # # 2. If e raised in runtime, e should be attached to ERROR_DATA here. + # if not hasattr(e, error.ERROR_DATA): + # # runtime error + # error.attach_error_data(e, in_runtime=True) + # raise + # except BaseException as e: + # error_data = getattr(e, error.ERROR_DATA, None) + # if error_data: + # error_data.raise_new_exception() + # else: + # logging_utils.warn( + # "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'" + # " if you can't handle this {} yourself.".format(type(e)) + # ) + # raise e + + # def get_func(self, dygraph_func): + # """ + # Returns a callable function which converts imperative dygraph APIs of + # the input dygraph_func into declarative net-building APIs, which means + # it doesn't return immediate digital result as get_output does. + # Users should handle Program and Executor by themselves. + + # Args: + # dygraph_func (callable): the dygraph function. + + # Returns: + # callable: converting imperative dygraph APIs into declarative + # net-building APIs. + + # Examples: + # .. code-block:: python + + # import paddle + + + # def func(x): + # if paddle.mean(x) > 0: + # x_v = x - 1 + # else: + # x_v = x + 1 + # return x_v + + + # prog_trans = paddle.jit.ProgramTranslator() + # static_func = prog_trans.get_func(func) + # print(callable(static_func)) # True + + # """ + # assert callable( + # dygraph_func + # ), "Input dygraph_func is not a callable in ProgramTranslator.get_func" + + # if not self.enable_to_static: + # logging_utils.warn( + # "The ProgramTranslator.get_func doesn't work when setting ProgramTranslator.enable to False. We will " + # "just return dygraph output. Please call ProgramTranslator.enable(True) if you would like to get static output." + # ) + # return dygraph_func + + # static_func = convert_to_static(dygraph_func) + # return static_func + + # def get_program(self, dygraph_func, *args, **kwargs): + # """ + # Returns the translated static program and input/output Tensors from + # dygraph function. The users can use the program to run by executor. + + # Args: + # dygraph_func (callable): the dygraph function. + # *args (tuple): the input argument of dygraph_func. + # **kwargs (dict): the input argument of dygraph_func. + + # Returns: + # tuple of (main_program, startup_program, inputs, outputs) whose + # types are (Program, Program, list of Tensors, list of Tensors). + # main_program: the converted main program. + # startup_program: the converted startup program. + # inputs: list of input Tensors which need to be fed. + # outputs: list of output Tensors which users can fetch. + + # Examples: + # .. code-block:: python + + # import paddle + + + # def func(x): + # if paddle.mean(x) > 0: + # x_v = x - 1 + # else: + # x_v = x + 1 + # return x_v + + + # prog_trans = paddle.jit.ProgramTranslator() + # x = paddle.ones([1, 2]) + # main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) + # print([i.name for i in inputs]) + # # [u'generated_tensor_0'] the feed input Tensor name representing x + # print([o.name for o in outputs]) + # # [u'_generated_var_4'] the fetch output Tensor name representing x_v + + # """ + # assert callable( + # dygraph_func + # ), "Input dygraph_func is not a callable in ProgramTranslator.get_program" + + # if not self.enable_to_static: + # logging_utils.warn( + # "The ProgramTranslator.get_program doesn't work when setting ProgramTranslator.enable to False." + # "We will just return dygraph output. " + # "Please call ProgramTranslator.enable(True) if you would like to get static output." + # ) + # return dygraph_func(*args, **kwargs) - if not self.enable_to_static: - logging_utils.warn( - "The ProgramTranslator.get_func doesn't work when setting ProgramTranslator.enable to False. We will " - "just return dygraph output. Please call ProgramTranslator.enable(True) if you would like to get static output." - ) - return dygraph_func + # function_spec = FunctionSpec(dygraph_func) + # cache_key = CacheKey.from_func_and_args( + # function_spec, args, kwargs, getattr(dygraph_func, '__self__', None) + # ) + # concrete_program, partial_program_layer = self._program_cache[cache_key] - static_func = convert_to_static(dygraph_func) - return static_func + # # Note: concrete_program hold all input/output infos include non-Variable + # input_vars = [ + # var + # for var in concrete_program.inputs + # if isinstance(var, framework.Variable) + # ] + # output_vars = [ + # var + # for var in concrete_program.outputs + # if isinstance(var, framework.Variable) + # ] - def get_program(self, dygraph_func, *args, **kwargs): - """ - Returns the translated static program and input/output Tensors from - dygraph function. The users can use the program to run by executor. + # return ( + # concrete_program.main_program, + # concrete_program.startup_program, + # input_vars, + # output_vars, + # ) + + # def get_code(self, dygraph_func): + # """ + # Returns the translated static function string code from dygraph function. + + # Args: + # dygraph_func (callable): the dygraph function. + + # Returns: + # str: the string code of translated static function. + + # Examples: + # .. code-block:: python + + # import paddle + + + # def func(x): + # if paddle.mean(x) > 0: + # x_v = x - 1 + # else: + # x_v = x + 1 + # return x_v + + + # prog_trans = paddle.jit.ProgramTranslator() - Args: - dygraph_func (callable): the dygraph function. - *args (tuple): the input argument of dygraph_func. - **kwargs (dict): the input argument of dygraph_func. + # code = prog_trans.get_code(func) + # print(type(code)) # - Returns: - tuple of (main_program, startup_program, inputs, outputs) whose - types are (Program, Program, list of Tensors, list of Tensors). - main_program: the converted main program. - startup_program: the converted startup program. - inputs: list of input Tensors which need to be fed. - outputs: list of output Tensors which users can fetch. + # """ + # assert callable( + # dygraph_func + # ), "Input dygraph_func is not a callable in ProgramTranslator.get_code" + # # Gets AST from dygraph function - Examples: - .. code-block:: python + # unwrap_func = unwrap(dygraph_func) + # raw_code = inspect.getsource(unwrap_func) + # code = textwrap.dedent(raw_code) + # root = gast.parse(code) - import paddle + # # Transform AST + # dygraph_to_static = DygraphToStaticAst() + # root_wrapper = dygraph_to_static.get_static_ast(root) + # # Get source_code + # source_code = ast_to_source_code(root_wrapper.node) + # return source_code + + # def get_program_cache(self): + # """ + # Returns the ProgramCache instance. This method is used by PaddlePaddle + # developers to manage program cache in ProgramTranslator. Normal users + # don't have to call this method. - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v + # Returns: + # ProgramCache: ProgramCache instance of ProgramTranslator. + # Examples: + # .. code-block:: python - prog_trans = paddle.jit.ProgramTranslator() - x = paddle.ones([1, 2]) - main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) - print([i.name for i in inputs]) - # [u'generated_tensor_0'] the feed input Tensor name representing x - print([o.name for o in outputs]) - # [u'_generated_var_4'] the fetch output Tensor name representing x_v + # import paddle - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_program" + # prog_trans = paddle.jit.ProgramTranslator() + # prog_cache = prog_trans.get_program_cache() - if not self.enable_to_static: - logging_utils.warn( - "The ProgramTranslator.get_program doesn't work when setting ProgramTranslator.enable to False." - "We will just return dygraph output. " - "Please call ProgramTranslator.enable(True) if you would like to get static output." - ) - return dygraph_func(*args, **kwargs) + # """ + # return self._program_cache - function_spec = FunctionSpec(dygraph_func) - cache_key = CacheKey.from_func_and_args( - function_spec, args, kwargs, getattr(dygraph_func, '__self__', None) - ) - concrete_program, partial_program_layer = self._program_cache[cache_key] - # Note: concrete_program hold all input/output infos include non-Variable - input_vars = [ - var - for var in concrete_program.inputs - if isinstance(var, framework.Variable) - ] - output_vars = [ - var - for var in concrete_program.outputs - if isinstance(var, framework.Variable) - ] - - return ( - concrete_program.main_program, - concrete_program.startup_program, - input_vars, - output_vars, - ) - - def get_code(self, dygraph_func): - """ - Returns the translated static function string code from dygraph function. +def enable_to_static(enable_to_static_bool): - Args: - dygraph_func (callable): the dygraph function. - - Returns: - str: the string code of translated static function. - - Examples: - .. code-block:: python - - import paddle - - - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v + """ + Enable or disable the converting from imperative to static graph by + ProgramTranslator globally. + Args: + enable_to_static_bool (bool): True or False to enable or disable converting to static. - prog_trans = paddle.jit.ProgramTranslator() + Returns: + None. - code = prog_trans.get_code(func) - print(type(code)) # + Examples: + .. code-block:: python - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_code" - # Gets AST from dygraph function - - unwrap_func = unwrap(dygraph_func) - raw_code = inspect.getsource(unwrap_func) - code = textwrap.dedent(raw_code) - root = gast.parse(code) - - # Transform AST - dygraph_to_static = DygraphToStaticAst() - root_wrapper = dygraph_to_static.get_static_ast(root) - - # Get source_code - source_code = ast_to_source_code(root_wrapper.node) - return source_code + import paddle - def get_program_cache(self): - """ - Returns the ProgramCache instance. This method is used by PaddlePaddle - developers to manage program cache in ProgramTranslator. Normal users - don't have to call this method. - Returns: - ProgramCache: ProgramCache instance of ProgramTranslator. + @paddle.jit.to_static + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v - Examples: - .. code-block:: python - import paddle + paddle.jit.enable_to_static(False) - prog_trans = paddle.jit.ProgramTranslator() - prog_cache = prog_trans.get_program_cache() + x = paddle.ones([1, 2]) + # ProgramTranslator is disabled so the func is run in dygraph + print(func(x)) # [[0. 0.]] - """ - return self._program_cache + """ + check_type( + enable_to_static_bool, + "enable_to_static_bool", + bool, + "paddle.jit.to_static", + ) + _program_trans = ProgramTranslator() + _program_trans.enable(enable_to_static_bool) \ No newline at end of file From 8493da5f2b29b08929f1e08e305c137566f84180 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 12:46:58 +0800 Subject: [PATCH 02/20] fix code style --- python/paddle/jit/__init__.py | 2 +- python/paddle/jit/api.py | 1 - python/paddle/jit/dy2static/program_translator.py | 12 +----------- 3 files changed, 2 insertions(+), 13 deletions(-) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index 2315a4c879540e..ceb61e602f5950 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -33,5 +33,5 @@ 'set_code_level', 'set_verbosity', 'not_to_static', - 'enable_to_static' + 'enable_to_static', ] diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index 4372f712a6ad8a..f55aeb5c1b8cc1 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -48,7 +48,6 @@ ProgramTranslator, StaticFunction, unwrap_decorators, - enable_to_static, ) from paddle.jit.translated_layer import ( TranslatedLayer, diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 2f45d004bb9cf0..fc0b2337ffaaa1 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -14,7 +14,6 @@ import collections import inspect -import textwrap import threading import weakref @@ -42,7 +41,6 @@ from .utils import ( ALREADY_D2S, ast_to_func, - ast_to_source_code, func_to_source_code, input_specs_compatible, make_hashable, @@ -1222,7 +1220,6 @@ def func(x): # import paddle - # def func(x): # if paddle.mean(x) > 0: # x_v = x - 1 @@ -1230,7 +1227,6 @@ def func(x): # x_v = x + 1 # return x_v - # prog_trans = paddle.jit.ProgramTranslator() # x = paddle.ones([1, 2]) @@ -1305,7 +1301,6 @@ def func(x): # import paddle - # def func(x): # if paddle.mean(x) > 0: # x_v = x - 1 @@ -1313,7 +1308,6 @@ def func(x): # x_v = x + 1 # return x_v - # prog_trans = paddle.jit.ProgramTranslator() # static_func = prog_trans.get_func(func) # print(callable(static_func)) # True @@ -1356,7 +1350,6 @@ def func(x): # import paddle - # def func(x): # if paddle.mean(x) > 0: # x_v = x - 1 @@ -1364,7 +1357,6 @@ def func(x): # x_v = x + 1 # return x_v - # prog_trans = paddle.jit.ProgramTranslator() # x = paddle.ones([1, 2]) # main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) @@ -1426,7 +1418,6 @@ def func(x): # import paddle - # def func(x): # if paddle.mean(x) > 0: # x_v = x - 1 @@ -1434,7 +1425,6 @@ def func(x): # x_v = x + 1 # return x_v - # prog_trans = paddle.jit.ProgramTranslator() # code = prog_trans.get_code(func) @@ -1521,4 +1511,4 @@ def func(x): "paddle.jit.to_static", ) _program_trans = ProgramTranslator() - _program_trans.enable(enable_to_static_bool) \ No newline at end of file + _program_trans.enable(enable_to_static_bool) From b85f33614e71dccea72e391573f438e28563ffa8 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 12:58:23 +0800 Subject: [PATCH 03/20] fix cant import enable_to_static and update unitest --- python/paddle/fluid/tests/unittests/test_base_layer.py | 4 +--- python/paddle/jit/api.py | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/test_base_layer.py b/python/paddle/fluid/tests/unittests/test_base_layer.py index bdb0d3076274fd..05d9b71c1e4372 100644 --- a/python/paddle/fluid/tests/unittests/test_base_layer.py +++ b/python/paddle/fluid/tests/unittests/test_base_layer.py @@ -20,7 +20,6 @@ import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable from paddle.fluid.framework import EagerParamBase, ParamBase, in_dygraph_mode -from paddle.jit import ProgramTranslator class L1(fluid.Layer): @@ -339,11 +338,10 @@ def forward(self, x): class TestModifiedBuffer(unittest.TestCase): def funcsetUp(self): paddle.disable_static() - self.prog_trans = ProgramTranslator() self.shape = [10, 16] def _run(self, to_static=False): - self.prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) x = paddle.ones([1], 'int32') net = BufferNetWithModification(self.shape) diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index f55aeb5c1b8cc1..c17567b10221ab 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -45,6 +45,7 @@ CONVERSION_OPTIONS, ) from .dy2static.program_translator import ( + enable_to_static, ProgramTranslator, StaticFunction, unwrap_decorators, From 2373cebd43eb8ffed9b86ab6498377cc6394521d Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 13:26:57 +0800 Subject: [PATCH 04/20] change unitest and rollback code of PT --- .../dygraph_to_static/test_assert.py | 3 +- .../dygraph_to_static/test_cycle_gan.py | 4 +- .../dygraph_to_static/test_declarative.py | 8 +- .../unittests/dygraph_to_static/test_grad.py | 5 +- .../dygraph_to_static/test_grid_generator.py | 3 +- .../unittests/dygraph_to_static/test_slice.py | 5 +- .../unittests/dygraph_to_static/test_tsm.py | 5 +- .../dygraph_to_static/test_word2vec.py | 5 +- .../fluid/tests/unittests/test_jit_layer.py | 6 +- python/paddle/jit/__init__.py | 4 +- .../jit/dy2static/program_translator.py | 517 +++++++++--------- python/paddle/tests/test_model.py | 5 +- 12 files changed, 269 insertions(+), 301 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_assert.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_assert.py index 0bfd19e7326784..55e1060104bdb2 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_assert.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_assert.py @@ -18,7 +18,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static @@ -35,7 +34,7 @@ def dyfunc_assert_non_variable(x=True): class TestAssertVariable(unittest.TestCase): def _run(self, func, x, with_exception, to_static): - ProgramTranslator().enable(to_static) + paddle.jit.enable_to_static(to_static) if with_exception: with self.assertRaises(BaseException): with fluid.dygraph.guard(): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py index 988a994a72d926..792845f16eb0be 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py @@ -38,7 +38,6 @@ import paddle from paddle.fluid.dygraph import to_variable -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import BatchNorm @@ -61,7 +60,6 @@ IMAGE_SIZE = 64 SEED = 2020 -program_translator = ProgramTranslator() class Cycle_Gan(fluid.dygraph.Layer): @@ -560,7 +558,7 @@ def train(args, to_static): else fluid.CPUPlace() ) - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(place): max_images_num = args.max_images_num diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py index cf9f094388d56c..35d21651aa47e6 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py @@ -22,7 +22,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph import Layer, to_variable -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.jit.dy2static.program_translator import ( ConcreteProgram, @@ -30,7 +29,6 @@ ) from paddle.static import InputSpec -program_trans = ProgramTranslator() class SimpleNet(Layer): @@ -210,7 +208,7 @@ def foo_func(a, b, c=1, d=2): class TestDifferentInputSpecCacheProgram(unittest.TestCase): def setUp(self): - program_trans.enable(True) + paddle.jit.enable_to_static(True) def test_with_different_input(self): with fluid.dygraph.guard(fluid.CPUPlace()): @@ -357,7 +355,7 @@ def test_error(self): with self.assertRaises(RuntimeError): func(np.ones(5).astype("int32")) - program_trans.enable(False) + paddle.jit.enable_to_static(False) with self.assertRaises(AssertionError): # AssertionError: We Only support to_variable in imperative mode, # please use fluid.dygraph.guard() as context to run it in imperative Mode @@ -367,7 +365,7 @@ def test_error(self): class TestDecorateModelDirectly(unittest.TestCase): def setUp(self): paddle.disable_static() - program_trans.enable(True) + paddle.jit.enable_to_static(True) self.x = to_variable(np.ones([4, 10]).astype('float32')) def test_fake_input(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grad.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grad.py index e359514e4c8d69..7f2d2e19f66b12 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grad.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grad.py @@ -74,10 +74,9 @@ def setUp(self): self.x.stop_gradient = False def _run(self, func, to_static): - prog_trans = paddle.jit.ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) ret = func(self.x).numpy() - prog_trans.enable(True) + paddle.jit.enable_to_static(True) return ret def test_forward(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grid_generator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grid_generator.py index f46ae0eb6bfa48..e4bf77d8b9a485 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grid_generator.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_grid_generator.py @@ -136,8 +136,7 @@ def setUp(self): self.x = paddle.uniform(shape=[1, 20, 2], dtype='float32') def _run(self, to_static): - prog_trans = paddle.jit.ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) net = GridGenerator(40, 20) ret = net(self.x, [32, 100]) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py index 87eb6e51e74a50..d0837245460030 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_slice.py @@ -23,7 +23,6 @@ SEED = 2020 np.random.seed(SEED) -prog_trans = paddle.jit.ProgramTranslator() @paddle.jit.to_static @@ -130,7 +129,7 @@ def run_dygraph_mode(self): return self._run(to_static=False) def _run(self, to_static): - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) res = self.dygraph_func(self.input) return res.numpy() @@ -177,7 +176,7 @@ def tearDown(self): self.temp_dir.cleanup() def test_set_value_with_save(self): - prog_trans.enable(True) + paddle.jit.enable_to_static(True) model = LayerWithSetValue(input_dim=10, hidden=1) x = paddle.full(shape=[5, 10], fill_value=5.0, dtype="float32") paddle.jit.save( diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py index 5e0c971c6ca1a1..2c6a99d277e46a 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py @@ -24,7 +24,7 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable -from paddle.jit import ProgramTranslator +# from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import BatchNorm, Linear @@ -290,8 +290,7 @@ def create_optimizer(cfg, params): def train(args, fake_data_reader, to_static): - program_translator = ProgramTranslator() - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) config = parse_config(args.config) train_config = merge_configs(config, 'train', vars(args)) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py index 61150231c578e1..6eb63eb293793b 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py @@ -20,7 +20,7 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator +# from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import Embedding @@ -278,8 +278,7 @@ def forward(self, center_words, target_words, label): def train(to_static): - program_translator = ProgramTranslator() - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) random.seed(0) np.random.seed(0) diff --git a/python/paddle/fluid/tests/unittests/test_jit_layer.py b/python/paddle/fluid/tests/unittests/test_jit_layer.py index c670ac00aed6c3..93380400d2afbc 100644 --- a/python/paddle/fluid/tests/unittests/test_jit_layer.py +++ b/python/paddle/fluid/tests/unittests/test_jit_layer.py @@ -20,7 +20,6 @@ import paddle from paddle.fluid.framework import _dygraph_place_guard -from paddle.jit.dy2static.program_translator import ProgramTranslator from paddle.jit.layer import Layer from paddle.static import InputSpec @@ -61,11 +60,10 @@ def test_multi_load(self): x = paddle.full([2, 4], 2) model = Net() - program_translator = ProgramTranslator() - program_translator.enable(False) + paddle.jit.enable_to_static(False) forward_out1 = model.forward(x) infer_out1 = model.infer(x) - program_translator.enable(True) + paddle.jit.enable_to_static(True) model_path = os.path.join(self.temp_dir.name, 'multi_program') paddle.jit.save(model, model_path, combine_params=True) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index ceb61e602f5950..35afe451b62fc2 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -21,14 +21,14 @@ from .dy2static.logging_utils import set_code_level, set_verbosity from . import dy2static -from .dy2static.program_translator import ProgramTranslator +from .dy2static.program_translator import ProgramTranslator # TODO(RyanHuang): Remove it from .translated_layer import TranslatedLayer __all__ = [ # noqa 'save', 'load', 'to_static', - 'ProgramTranslator', + 'ProgramTranslator', # TODO(RyanHuang): Remove it 'TranslatedLayer', 'set_code_level', 'set_verbosity', diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index fc0b2337ffaaa1..9283bc65da8e0a 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -16,6 +16,7 @@ import inspect import threading import weakref +import textwrap from paddle.fluid import _non_static_mode, framework from paddle.fluid.data_feeder import check_type @@ -24,7 +25,7 @@ from paddle.fluid.layers.utils import flatten from paddle.utils import gast -from . import error, logging_utils +from . import error, logging_utils, ast_to_source_code from .ast_transformer import DygraphToStaticAst from .function_spec import ( FunctionSpec, @@ -1201,273 +1202,253 @@ def func(x): ) self.enable_to_static = enable_to_static - # def get_output(self, dygraph_func, *args, **kwargs): - # """ - # Returns the output dygraph Tensor for dygraph function. The dygraph - # function will be translated into static graph function so the under - # beneath numerical result will be calculated by static graph mode. - - # Args: - # dygraph_func (callable): the dygraph function. - # *args (tuple): the input argument of dygraph_func. - # **kwargs (dict): the input argument of dygraph_func. - - # Returns: - # Tensor or tuple of Tensors: the dygraph Tensor containing digital result. - - # Examples: - # .. code-block:: python - - # import paddle - - # def func(x): - # if paddle.mean(x) > 0: - # x_v = x - 1 - # else: - # x_v = x + 1 - # return x_v - - # prog_trans = paddle.jit.ProgramTranslator() - - # x = paddle.ones([1, 2]) - # x_v = prog_trans.get_output(func, x) - # print(x_v) # [[0. 0.]] - - # """ - # assert callable( - # dygraph_func - # ), "Input dygraph_func is not a callable in ProgramTranslator.get_output" - - # if not self.enable_to_static: - # # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) - # # will show up **only once**. - # logging_utils.warn( - # "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. " - # "We will just return dygraph output. " - # "Please call ProgramTranslator.enable(True) if you would like to get static output." - # ) - # return dygraph_func(*args, **kwargs) - # try: - # function_spec = FunctionSpec(dygraph_func) - # cache_key = CacheKey.from_func_and_args( - # function_spec, - # args, - # kwargs, - # getattr(dygraph_func, '__self__', None), - # ) - # _, partial_program_layer = self._program_cache[cache_key] - - # if args and isinstance(args[0], layers.Layer): - # # Synchronize self.training attribute. - # partial_program_layer.training = args[0].training - # args = args[1:] - # try: - # return partial_program_layer(args) - # except BaseException as e: - # # NOTE: - # # 1. If e is raised in compile time, e should have been attached to ERROR_DATA before; - # # 2. If e raised in runtime, e should be attached to ERROR_DATA here. - # if not hasattr(e, error.ERROR_DATA): - # # runtime error - # error.attach_error_data(e, in_runtime=True) - # raise - # except BaseException as e: - # error_data = getattr(e, error.ERROR_DATA, None) - # if error_data: - # error_data.raise_new_exception() - # else: - # logging_utils.warn( - # "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'" - # " if you can't handle this {} yourself.".format(type(e)) - # ) - # raise e - - # def get_func(self, dygraph_func): - # """ - # Returns a callable function which converts imperative dygraph APIs of - # the input dygraph_func into declarative net-building APIs, which means - # it doesn't return immediate digital result as get_output does. - # Users should handle Program and Executor by themselves. - - # Args: - # dygraph_func (callable): the dygraph function. - - # Returns: - # callable: converting imperative dygraph APIs into declarative - # net-building APIs. - - # Examples: - # .. code-block:: python - - # import paddle - - # def func(x): - # if paddle.mean(x) > 0: - # x_v = x - 1 - # else: - # x_v = x + 1 - # return x_v - - # prog_trans = paddle.jit.ProgramTranslator() - # static_func = prog_trans.get_func(func) - # print(callable(static_func)) # True - - # """ - # assert callable( - # dygraph_func - # ), "Input dygraph_func is not a callable in ProgramTranslator.get_func" - - # if not self.enable_to_static: - # logging_utils.warn( - # "The ProgramTranslator.get_func doesn't work when setting ProgramTranslator.enable to False. We will " - # "just return dygraph output. Please call ProgramTranslator.enable(True) if you would like to get static output." - # ) - # return dygraph_func - - # static_func = convert_to_static(dygraph_func) - # return static_func - - # def get_program(self, dygraph_func, *args, **kwargs): - # """ - # Returns the translated static program and input/output Tensors from - # dygraph function. The users can use the program to run by executor. - - # Args: - # dygraph_func (callable): the dygraph function. - # *args (tuple): the input argument of dygraph_func. - # **kwargs (dict): the input argument of dygraph_func. - - # Returns: - # tuple of (main_program, startup_program, inputs, outputs) whose - # types are (Program, Program, list of Tensors, list of Tensors). - # main_program: the converted main program. - # startup_program: the converted startup program. - # inputs: list of input Tensors which need to be fed. - # outputs: list of output Tensors which users can fetch. - - # Examples: - # .. code-block:: python - - # import paddle - - # def func(x): - # if paddle.mean(x) > 0: - # x_v = x - 1 - # else: - # x_v = x + 1 - # return x_v - - # prog_trans = paddle.jit.ProgramTranslator() - # x = paddle.ones([1, 2]) - # main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) - # print([i.name for i in inputs]) - # # [u'generated_tensor_0'] the feed input Tensor name representing x - # print([o.name for o in outputs]) - # # [u'_generated_var_4'] the fetch output Tensor name representing x_v - - # """ - # assert callable( - # dygraph_func - # ), "Input dygraph_func is not a callable in ProgramTranslator.get_program" - - # if not self.enable_to_static: - # logging_utils.warn( - # "The ProgramTranslator.get_program doesn't work when setting ProgramTranslator.enable to False." - # "We will just return dygraph output. " - # "Please call ProgramTranslator.enable(True) if you would like to get static output." - # ) - # return dygraph_func(*args, **kwargs) - - # function_spec = FunctionSpec(dygraph_func) - # cache_key = CacheKey.from_func_and_args( - # function_spec, args, kwargs, getattr(dygraph_func, '__self__', None) - # ) - # concrete_program, partial_program_layer = self._program_cache[cache_key] - - # # Note: concrete_program hold all input/output infos include non-Variable - # input_vars = [ - # var - # for var in concrete_program.inputs - # if isinstance(var, framework.Variable) - # ] - # output_vars = [ - # var - # for var in concrete_program.outputs - # if isinstance(var, framework.Variable) - # ] - - # return ( - # concrete_program.main_program, - # concrete_program.startup_program, - # input_vars, - # output_vars, - # ) - - # def get_code(self, dygraph_func): - # """ - # Returns the translated static function string code from dygraph function. - - # Args: - # dygraph_func (callable): the dygraph function. - - # Returns: - # str: the string code of translated static function. - - # Examples: - # .. code-block:: python - - # import paddle - - # def func(x): - # if paddle.mean(x) > 0: - # x_v = x - 1 - # else: - # x_v = x + 1 - # return x_v - - # prog_trans = paddle.jit.ProgramTranslator() - - # code = prog_trans.get_code(func) - # print(type(code)) # - - # """ - # assert callable( - # dygraph_func - # ), "Input dygraph_func is not a callable in ProgramTranslator.get_code" - # # Gets AST from dygraph function - - # unwrap_func = unwrap(dygraph_func) - # raw_code = inspect.getsource(unwrap_func) - # code = textwrap.dedent(raw_code) - # root = gast.parse(code) - - # # Transform AST - # dygraph_to_static = DygraphToStaticAst() - # root_wrapper = dygraph_to_static.get_static_ast(root) - - # # Get source_code - # source_code = ast_to_source_code(root_wrapper.node) - # return source_code - - # def get_program_cache(self): - # """ - # Returns the ProgramCache instance. This method is used by PaddlePaddle - # developers to manage program cache in ProgramTranslator. Normal users - # don't have to call this method. - - # Returns: - # ProgramCache: ProgramCache instance of ProgramTranslator. - - # Examples: - # .. code-block:: python - - # import paddle - - # prog_trans = paddle.jit.ProgramTranslator() - # prog_cache = prog_trans.get_program_cache() - - # """ - # return self._program_cache + def get_output(self, dygraph_func, *args, **kwargs): + """ + Returns the output dygraph Tensor for dygraph function. The dygraph + function will be translated into static graph function so the under + beneath numerical result will be calculated by static graph mode. + + Args: + dygraph_func (callable): the dygraph function. + *args (tuple): the input argument of dygraph_func. + **kwargs (dict): the input argument of dygraph_func. + + Returns: + Tensor or tuple of Tensors: the dygraph Tensor containing digital result. + + Examples: + .. code-block:: python + + import paddle + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + prog_trans = paddle.jit.ProgramTranslator() + + x = paddle.ones([1, 2]) + x_v = prog_trans.get_output(func, x) + print(x_v) # [[0. 0.]] + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_output" + + if not self.enable_to_static: + # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) + # will show up **only once**. + logging_utils.warn( + "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. " + "We will just return dygraph output. " + "Please call ProgramTranslator.enable(True) if you would like to get static output." + ) + return dygraph_func(*args, **kwargs) + try: + function_spec = FunctionSpec(dygraph_func) + cache_key = CacheKey.from_func_and_args( + function_spec, + args, + kwargs, + getattr(dygraph_func, '__self__', None), + ) + _, partial_program_layer = self._program_cache[cache_key] + + if args and isinstance(args[0], layers.Layer): + # Synchronize self.training attribute. + partial_program_layer.training = args[0].training + args = args[1:] + try: + return partial_program_layer(args) + except BaseException as e: + # NOTE: + # 1. If e is raised in compile time, e should have been attached to ERROR_DATA before; + # 2. If e raised in runtime, e should be attached to ERROR_DATA here. + if not hasattr(e, error.ERROR_DATA): + # runtime error + error.attach_error_data(e, in_runtime=True) + raise + except BaseException as e: + error_data = getattr(e, error.ERROR_DATA, None) + if error_data: + error_data.raise_new_exception() + else: + logging_utils.warn( + "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'" + " if you can't handle this {} yourself.".format(type(e)) + ) + raise e + + def get_func(self, dygraph_func): + """ + Returns a callable function which converts imperative dygraph APIs of + the input dygraph_func into declarative net-building APIs, which means + it doesn't return immediate digital result as get_output does. + Users should handle Program and Executor by themselves. + + Args: + dygraph_func (callable): the dygraph function. + + Returns: + callable: converting imperative dygraph APIs into declarative + net-building APIs. + + Examples: + .. code-block:: python + + import paddle + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + prog_trans = paddle.jit.ProgramTranslator() + static_func = prog_trans.get_func(func) + print(callable(static_func)) # True + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_func" + + if not self.enable_to_static: + logging_utils.warn( + "The ProgramTranslator.get_func doesn't work when setting ProgramTranslator.enable to False. We will " + "just return dygraph output. Please call ProgramTranslator.enable(True) if you would like to get static output." + ) + return dygraph_func + + static_func = convert_to_static(dygraph_func) + return static_func + + def get_program(self, dygraph_func, *args, **kwargs): + """ + Returns the translated static program and input/output Tensors from + dygraph function. The users can use the program to run by executor. + + Args: + dygraph_func (callable): the dygraph function. + *args (tuple): the input argument of dygraph_func. + **kwargs (dict): the input argument of dygraph_func. + + Returns: + tuple of (main_program, startup_program, inputs, outputs) whose + types are (Program, Program, list of Tensors, list of Tensors). + main_program: the converted main program. + startup_program: the converted startup program. + inputs: list of input Tensors which need to be fed. + outputs: list of output Tensors which users can fetch. + + Examples: + .. code-block:: python + + import paddle + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + prog_trans = paddle.jit.ProgramTranslator() + x = paddle.ones([1, 2]) + main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) + print([i.name for i in inputs]) + # [u'generated_tensor_0'] the feed input Tensor name representing x + print([o.name for o in outputs]) + # [u'_generated_var_4'] the fetch output Tensor name representing x_v + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_program" + + if not self.enable_to_static: + logging_utils.warn( + "The ProgramTranslator.get_program doesn't work when setting ProgramTranslator.enable to False." + "We will just return dygraph output. " + "Please call ProgramTranslator.enable(True) if you would like to get static output." + ) + return dygraph_func(*args, **kwargs) + + function_spec = FunctionSpec(dygraph_func) + cache_key = CacheKey.from_func_and_args( + function_spec, args, kwargs, getattr(dygraph_func, '__self__', None) + ) + concrete_program, partial_program_layer = self._program_cache[cache_key] + + # Note: concrete_program hold all input/output infos include non-Variable + input_vars = [ + var + for var in concrete_program.inputs + if isinstance(var, framework.Variable) + ] + output_vars = [ + var + for var in concrete_program.outputs + if isinstance(var, framework.Variable) + ] + + return ( + concrete_program.main_program, + concrete_program.startup_program, + input_vars, + output_vars, + ) + + def get_code(self, dygraph_func): + """ + Returns the translated static function string code from dygraph function. + + Args: + dygraph_func (callable): the dygraph function. + + Returns: + str: the string code of translated static function. + + Examples: + .. code-block:: python + + import paddle + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + prog_trans = paddle.jit.ProgramTranslator() + + code = prog_trans.get_code(func) + print(type(code)) # + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_code" + # Gets AST from dygraph function + + unwrap_func = unwrap(dygraph_func) + raw_code = inspect.getsource(unwrap_func) + code = textwrap.dedent(raw_code) + root = gast.parse(code) + + # Transform AST + dygraph_to_static = DygraphToStaticAst() + root_wrapper = dygraph_to_static.get_static_ast(root) + + # Get source_code + source_code = ast_to_source_code(root_wrapper.node) + return source_code def enable_to_static(enable_to_static_bool): diff --git a/python/paddle/tests/test_model.py b/python/paddle/tests/test_model.py index 7094ea5d59a4d1..9090c28306a05d 100644 --- a/python/paddle/tests/test_model.py +++ b/python/paddle/tests/test_model.py @@ -25,7 +25,6 @@ from paddle import Model, fluid, to_tensor from paddle.hapi.model import prepare_distributed_context from paddle.io import Dataset, DistributedBatchSampler -from paddle.jit.dy2static.program_translator import ProgramTranslator from paddle.metric import Accuracy from paddle.nn import Conv2D, Linear, ReLU, Sequential from paddle.nn.layer.loss import CrossEntropyLoss @@ -826,8 +825,8 @@ def test_export_deploy_model(self): for dynamic in [True, False]: paddle.disable_static() if dynamic else None - prog_translator = ProgramTranslator() - prog_translator.enable(False) if not dynamic else None + paddle.jit.enable_to_static(False) if not dynamic else None + net = LeNet() inputs = [InputSpec([None, 1, 28, 28], 'float32', 'x')] model = Model(net, inputs) From 9482597a3d08a1868c3685b0fc1c99d7851ffa48 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 13:53:59 +0800 Subject: [PATCH 05/20] fix can't import as of utils --- .../tests/unittests/dygraph_to_static/test_cycle_gan.py | 1 - .../tests/unittests/dygraph_to_static/test_declarative.py | 1 - .../fluid/tests/unittests/dygraph_to_static/test_tsm.py | 1 + .../tests/unittests/dygraph_to_static/test_word2vec.py | 1 + python/paddle/jit/__init__.py | 6 ++++-- python/paddle/jit/dy2static/__init__.py | 1 + python/paddle/jit/dy2static/program_translator.py | 4 ++-- 7 files changed, 9 insertions(+), 6 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py index 792845f16eb0be..0701750e3011a7 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cycle_gan.py @@ -61,7 +61,6 @@ SEED = 2020 - class Cycle_Gan(fluid.dygraph.Layer): def __init__(self, input_channel, istrain=True): super().__init__() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py index 35d21651aa47e6..2f9287a315c0f7 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py @@ -30,7 +30,6 @@ from paddle.static import InputSpec - class SimpleNet(Layer): def __init__(self): super().__init__() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py index 2c6a99d277e46a..412b1c9489cb22 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py @@ -24,6 +24,7 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable + # from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import BatchNorm, Linear diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py index 6eb63eb293793b..512103d361cd4c 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py @@ -20,6 +20,7 @@ import paddle import paddle.fluid as fluid + # from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import Embedding diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index 35afe451b62fc2..f218419a867dad 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -21,14 +21,16 @@ from .dy2static.logging_utils import set_code_level, set_verbosity from . import dy2static -from .dy2static.program_translator import ProgramTranslator # TODO(RyanHuang): Remove it +from .dy2static.program_translator import ( + ProgramTranslator, +) # TODO(RyanHuang): Remove it from .translated_layer import TranslatedLayer __all__ = [ # noqa 'save', 'load', 'to_static', - 'ProgramTranslator', # TODO(RyanHuang): Remove it + 'ProgramTranslator', # TODO(RyanHuang): Remove it 'TranslatedLayer', 'set_code_level', 'set_verbosity', diff --git a/python/paddle/jit/dy2static/__init__.py b/python/paddle/jit/dy2static/__init__.py index b55d5d672c2b11..ff1dbb14f6da58 100644 --- a/python/paddle/jit/dy2static/__init__.py +++ b/python/paddle/jit/dy2static/__init__.py @@ -15,6 +15,7 @@ from .utils import ( saw, UndefinedVar, + ast_to_source_code ) from .convert_operators import convert_logical_and as And # noqa: F401 from .convert_operators import convert_var_dtype as AsDtype # noqa: F401 diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 9283bc65da8e0a..a2c5ebb2ea9907 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -14,9 +14,9 @@ import collections import inspect +import textwrap import threading import weakref -import textwrap from paddle.fluid import _non_static_mode, framework from paddle.fluid.data_feeder import check_type @@ -25,7 +25,7 @@ from paddle.fluid.layers.utils import flatten from paddle.utils import gast -from . import error, logging_utils, ast_to_source_code +from . import ast_to_source_code, error, logging_utils from .ast_transformer import DygraphToStaticAst from .function_spec import ( FunctionSpec, From 81f677d01d95c86128f902ef45c0a8db3d27109a Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 14:28:13 +0800 Subject: [PATCH 06/20] roll back PT --- .../jit/dy2static/program_translator.py | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index a2c5ebb2ea9907..746c06c0b8d2e2 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -1157,7 +1157,7 @@ def __init__(self): if self._initialized: return self._initialized = True - # self._program_cache = ProgramCache() + self._program_cache = ProgramCache() self.enable_to_static = True def enable(self, enable_to_static): @@ -1221,6 +1221,7 @@ def get_output(self, dygraph_func, *args, **kwargs): import paddle + def func(x): if paddle.mean(x) > 0: x_v = x - 1 @@ -1228,6 +1229,7 @@ def func(x): x_v = x + 1 return x_v + prog_trans = paddle.jit.ProgramTranslator() x = paddle.ones([1, 2]) @@ -1302,6 +1304,7 @@ def get_func(self, dygraph_func): import paddle + def func(x): if paddle.mean(x) > 0: x_v = x - 1 @@ -1309,6 +1312,7 @@ def func(x): x_v = x + 1 return x_v + prog_trans = paddle.jit.ProgramTranslator() static_func = prog_trans.get_func(func) print(callable(static_func)) # True @@ -1351,6 +1355,7 @@ def get_program(self, dygraph_func, *args, **kwargs): import paddle + def func(x): if paddle.mean(x) > 0: x_v = x - 1 @@ -1358,6 +1363,7 @@ def func(x): x_v = x + 1 return x_v + prog_trans = paddle.jit.ProgramTranslator() x = paddle.ones([1, 2]) main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) @@ -1419,6 +1425,7 @@ def get_code(self, dygraph_func): import paddle + def func(x): if paddle.mean(x) > 0: x_v = x - 1 @@ -1426,6 +1433,7 @@ def func(x): x_v = x + 1 return x_v + prog_trans = paddle.jit.ProgramTranslator() code = prog_trans.get_code(func) @@ -1450,6 +1458,27 @@ def func(x): source_code = ast_to_source_code(root_wrapper.node) return source_code + def get_program_cache(self): + """ + Returns the ProgramCache instance. This method is used by PaddlePaddle + developers to manage program cache in ProgramTranslator. Normal users + don't have to call this method. + + Returns: + ProgramCache: ProgramCache instance of ProgramTranslator. + + Examples: + .. code-block:: python + + import paddle + + prog_trans = paddle.jit.ProgramTranslator() + prog_cache = prog_trans.get_program_cache() + + """ + return self._program_cache + + def enable_to_static(enable_to_static_bool): From aad66f99986941b22ddd9323299a287f46af3081 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 14:29:42 +0800 Subject: [PATCH 07/20] fix roll back --- python/paddle/jit/dy2static/__init__.py | 6 +----- python/paddle/jit/dy2static/program_translator.py | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/python/paddle/jit/dy2static/__init__.py b/python/paddle/jit/dy2static/__init__.py index ff1dbb14f6da58..b3e70f48700315 100644 --- a/python/paddle/jit/dy2static/__init__.py +++ b/python/paddle/jit/dy2static/__init__.py @@ -12,11 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .utils import ( - saw, - UndefinedVar, - ast_to_source_code -) +from .utils import saw, UndefinedVar, ast_to_source_code from .convert_operators import convert_logical_and as And # noqa: F401 from .convert_operators import convert_var_dtype as AsDtype # noqa: F401 from .convert_operators import convert_assert as Assert # noqa: F401 diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 746c06c0b8d2e2..8924692ad82e4d 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -1479,7 +1479,6 @@ def get_program_cache(self): return self._program_cache - def enable_to_static(enable_to_static_bool): """ From 0e21b61c6d7986483256628418092d12401d0e43 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 15:45:05 +0800 Subject: [PATCH 08/20] add some unitest --- .../unittests/dygraph_to_static/test_bert.py | 8 +++----- .../unittests/dygraph_to_static/test_bmn.py | 7 +++---- .../dygraph_to_static/test_break_continue.py | 5 ++--- .../dygraph_to_static/test_build_strategy.py | 5 +---- .../dygraph_to_static/test_cache_program.py | 4 +--- .../dygraph_to_static/test_convert_call.py | 9 ++++----- .../unittests/dygraph_to_static/test_dict.py | 6 +++--- .../dygraph_to_static/test_fetch_feed.py | 3 +-- .../dygraph_to_static/test_for_enumerate.py | 6 ++---- .../unittests/dygraph_to_static/test_ifelse.py | 16 ++++++++-------- .../dygraph_to_static/test_isinstance.py | 3 +-- .../unittests/dygraph_to_static/test_lac.py | 5 ++--- .../unittests/dygraph_to_static/test_logical.py | 4 +--- .../dygraph_to_static/test_mobile_net.py | 5 ++--- .../dygraph_to_static/test_partial_program.py | 1 - .../unittests/dygraph_to_static/test_print.py | 5 ++--- .../unittests/dygraph_to_static/test_ptb_lm.py | 6 ++---- .../dygraph_to_static/test_ptb_lm_v2.py | 5 ++--- .../test_reinforcement_learning.py | 4 +--- .../unittests/dygraph_to_static/test_resnet.py | 6 ++---- .../dygraph_to_static/test_resnet_amp.py | 4 +--- .../dygraph_to_static/test_resnet_pure_fp16.py | 3 +-- .../dygraph_to_static/test_resnet_v2.py | 5 ++--- .../unittests/dygraph_to_static/test_return.py | 3 +-- .../test_save_inference_model.py | 5 +---- .../dygraph_to_static/test_save_load.py | 6 ++---- .../dygraph_to_static/test_se_resnet.py | 7 ++----- .../dygraph_to_static/test_sentiment.py | 4 +--- .../unittests/dygraph_to_static/test_seq2seq.py | 6 ++---- .../unittests/dygraph_to_static/test_simnet.py | 3 +-- .../dygraph_to_static/test_simnet_v2.py | 3 +-- .../dygraph_to_static/test_tensor_methods.py | 12 ++++-------- .../unittests/dygraph_to_static/test_tsm.py | 1 - .../unittests/dygraph_to_static/test_word2vec.py | 1 - .../unittests/dygraph_to_static/test_yolov3.py | 4 +--- .../paddle/jit/dy2static/program_translator.py | 3 +-- 36 files changed, 64 insertions(+), 119 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bert.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bert.py index 863ed57b86a6fe..88054e689fc0e1 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bert.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bert.py @@ -24,10 +24,8 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX -program_translator = ProgramTranslator() place = ( fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() ) @@ -127,11 +125,11 @@ def train(self, bert_config, data_reader, to_static): return loss, ppl def train_dygraph(self, bert_config, data_reader): - program_translator.enable(False) + paddle.jit.enable_to_static(False) return self.train(bert_config, data_reader, False) def train_static(self, bert_config, data_reader): - program_translator.enable(True) + paddle.jit.enable_to_static(True) return self.train(bert_config, data_reader, True) def predict_static(self, data): @@ -157,7 +155,7 @@ def predict_static(self, data): return pred_res def predict_dygraph(self, bert_config, data): - program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(place): bert = PretrainModelLayer( config=bert_config, weight_sharing=False, use_fp16=False diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py index ae1c1327ccd49a..a6a9d7281208dc 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_bmn.py @@ -24,12 +24,11 @@ import paddle.fluid as fluid from paddle.fluid import ParamAttr from paddle.fluid.dygraph import to_variable -from paddle.jit import ProgramTranslator, to_static +from paddle.jit import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX SEED = 2000 DATATYPE = 'float32' -program_translator = ProgramTranslator() # Note: Set True to eliminate randomness. # 1. For one operation, cuDNN has several algorithms, @@ -662,7 +661,7 @@ def tearDown(self): self.temp_dir.cleanup() def train_bmn(self, args, place, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) loss_data = [] with fluid.dygraph.guard(place): @@ -822,7 +821,7 @@ def verify_predict(self): break def predict_dygraph(self, data): - program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(self.place): bmn = BMN(self.args) # load dygraph trained parameters diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py index 209f6acbcd168e..499f7285f29aad 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py @@ -19,7 +19,6 @@ import paddle import paddle.fluid as fluid from paddle.jit.api import to_static -from paddle.jit.dy2static.program_translator import ProgramTranslator from paddle.jit.dy2static.utils import Dygraph2StaticException SEED = 2020 @@ -35,10 +34,10 @@ def setUp(self): def test_error(self): if self.dyfunc: with self.assertRaisesRegex(Dygraph2StaticException, self.error): - ProgramTranslator().enable(True) + paddle.jit.enable_to_static(True) self.assertTrue(to_static(self.dyfunc)(self.x)) paddle.fluid.dygraph.base._in_declarative_mode_ = False - ProgramTranslator().enable(False) + paddle.jit.enable_to_static(False) def test_continue_in_for(x): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_build_strategy.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_build_strategy.py index 96ae39c0cc0d19..13fb22421d3658 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_build_strategy.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_build_strategy.py @@ -18,9 +18,6 @@ from test_resnet import ResNetHelper import paddle -from paddle.jit import ProgramTranslator - -program_translator = ProgramTranslator() class TestResnetWithPass(unittest.TestCase): @@ -35,7 +32,7 @@ def setUp(self): paddle.fluid.set_flags({"FLAGS_max_inplace_grad_add": 8}) def train(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) return self.resnet_helper.train(to_static, self.build_strategy) def verify_predict(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cache_program.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cache_program.py index d4b7c603a1f8af..68698794f1a1c7 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cache_program.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_cache_program.py @@ -20,7 +20,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.jit.dy2static import convert_to_static @@ -91,8 +90,7 @@ def train_dygraph(self): return self.train(to_static=False) def train(self, to_static=False): - prog_trans = ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(fluid.CPUPlace()): dygraph_net = self.dygraph_class() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py index ad08aa1e3ccae5..83142fc826cb75 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py @@ -24,7 +24,6 @@ from paddle.jit import ProgramTranslator from paddle.jit.dy2static.convert_call_func import CONVERSION_OPTIONS -program_translator = ProgramTranslator() SEED = 2020 np.random.seed(SEED) @@ -93,13 +92,13 @@ def init_test_func(self): self.dyfunc = nested_func def get_dygraph_output(self): - program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(): res = self.dyfunc(self.input).numpy() return res def get_static_output(self): - program_translator.enable(True) + paddle.jit.enable_to_static(True) with fluid.dygraph.guard(): res = self.dyfunc(self.input).numpy() return res @@ -193,11 +192,11 @@ def _run(self): return res.numpy() def get_dygraph_output(self): - program_translator.enable(False) + paddle.jit.enable_to_static(False) return self._run() def get_static_output(self): - program_translator.enable(True) + paddle.jit.enable_to_static(True) return self._run() def test_transformed_static_result(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py index 2c882fc332e706..9ef0af8cc221c3 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py @@ -136,7 +136,7 @@ def _run_dygraph(self): def train(self, to_static=False): prog_trans = ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(PLACE): net = MainNetWithDict(batch_size=self.batch_size) ret = net(self.x) @@ -192,7 +192,7 @@ def _run_dygraph(self): def _run(self, to_static): prog_trans = ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) result = self.dygraph_func(self.input) @@ -238,7 +238,7 @@ def setUp(self): def train(self, to_static=False): prog_trans = ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(PLACE): net = NetWithDictPop() ret = net(z=0, x=self.x, y=True) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py index 6cd2c8bd17465a..dc49376344967d 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py @@ -68,8 +68,7 @@ def setUp(self): self.data = np.random.random((1, 2, 4, 4)).astype('float32') def train(self, to_static=False): - program_translator = ProgramTranslator() - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(): dy_layer = self.dygraph_class() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py index ed2480ab85232d..bf657e42f61a68 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py @@ -20,10 +20,8 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.static import InputSpec -program_translator = ProgramTranslator() # 0. for in range var.numpy()[0] @@ -363,7 +361,7 @@ def set_test_func(self): ) def _run(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(): return self.dygraph_func(self.input) @@ -390,7 +388,7 @@ def transformed_result_compare(self): class TestTransformForOriginalList(TestTransform): def _run(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(): return self.dygraph_func() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py index d06ba1b2ec26fb..8f3e97b0a8cb02 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py @@ -64,10 +64,10 @@ def setUp(self): def test_error(self): if self.dyfunc: with self.assertRaisesRegex(Dygraph2StaticException, self.error): - ProgramTranslator().enable(True) + paddle.jit.enable_to_static(True) self.assertTrue(paddle.jit.to_static(self.dyfunc)(self.x)) paddle.fluid.dygraph.base._in_declarative_mode_ = False - ProgramTranslator().enable(False) + paddle.jit.enable_to_static(False) class TestDygraphIfElse(unittest.TestCase): @@ -255,7 +255,7 @@ def _run_dygraph(self): def _run(self, to_static=False): prog_trans = ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(place): net = self.Net() @@ -365,7 +365,7 @@ def init_net(self): def _run(self, mode, to_static): prog_trans = ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) net = self.Net(mode) ret = net(self.x, self.y) @@ -423,10 +423,10 @@ def setUp(self): self.out = self.get_dy2stat_out() def get_dy2stat_out(self): - ProgramTranslator().enable(True) + paddle.jit.enable_to_static(True) static_func = paddle.jit.to_static(self.dyfunc) out = static_func(self.x) - ProgramTranslator().enable(False) + paddle.jit.enable_to_static(False) return out def test_ast_to_func(self): @@ -457,7 +457,7 @@ def setUp(self): self.dyfunc = dyfunc_ifelse_ret_int4 def test_ast_to_func(self): - ProgramTranslator().enable(True) + paddle.jit.enable_to_static(True) with self.assertRaises(Dygraph2StaticException): static_func = paddle.jit.to_static(self.dyfunc) out = static_func(self.x) @@ -467,7 +467,7 @@ def test_ast_to_func(self): # an exception is thrown during Dy2St, making the `_in_declarative_mode_` # a wrong value. So We need set `_in_declarative_mode_` to False manually. paddle.fluid.dygraph.base._in_declarative_mode_ = False - ProgramTranslator().enable(False) + paddle.jit.enable_to_static(False) class IfElseNet(paddle.nn.Layer): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_isinstance.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_isinstance.py index 490dda5bc40493..cb4c156b855b19 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_isinstance.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_isinstance.py @@ -75,8 +75,7 @@ def forward(self, x): def train(model, to_static): - prog_trans = paddle.jit.ProgramTranslator.get_instance() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) x = paddle.ones(shape=[2, 3], dtype='int32') out = model(x) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py index 8e877f39bfe956..5aeb33bb28a1c4 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py @@ -33,7 +33,6 @@ SEED = 2020 -program_translator = ProgramTranslator() # Add InputSpec to make unittest run faster. input_specs = [ paddle.static.InputSpec([None, None], 'int64'), @@ -542,7 +541,7 @@ def setUp(self): self.dy_param_path = os.path.join(self.temp_dir.name, 'lac_dy_param') def train(self, args, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) place = ( fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() @@ -656,7 +655,7 @@ def verify_predict(self): def predict_dygraph(self, batch): words, targets, length = batch - program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(self.place): model = LexNet(self.args) # load dygraph trained parameters diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py index fd335c41ba24ef..cb4c3ea827294d 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py @@ -21,11 +21,9 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.jit.dy2static.logical_transformer import cmpop_node_to_str from paddle.utils import gast -program_translator = ProgramTranslator() SEED = 2020 np.random.seed(22) @@ -186,7 +184,7 @@ def _set_test_func(self): ) def _run(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(self.place): result = self.dygraph_func(self.input) return result.numpy() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py index 6521dc31fc8caf..dfbe8999c1313a 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py @@ -36,7 +36,6 @@ fluid.set_flags({'FLAGS_cudnn_deterministic': True}) SEED = 2020 -program_translator = ProgramTranslator() class ConvBNLayer(fluid.dygraph.Layer): @@ -494,7 +493,7 @@ class Args: def train_mobilenet(args, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(args.place): np.random.seed(SEED) @@ -605,7 +604,7 @@ def predict_static(args, data): def predict_dygraph(args, data): - program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(args.place): if args.model == "MobileNetV1": model = MobileNetV1(class_dim=args.class_dim, scale=1.0) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py index cd172d8c97c2f8..6d5535848f56a8 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py @@ -130,7 +130,6 @@ def test_nest(self): class TestWithTrainAndEval(unittest.TestCase): def test_switch_eval_and_train(self): - program_translator = ProgramTranslator() with fluid.dygraph.guard(): linear_net = Linear() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py index 21de18a9f531dd..913a080243a3a1 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py @@ -18,9 +18,8 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator, to_static +from paddle.jit import to_static -program_translator = ProgramTranslator() # 1. print Tensor @@ -99,7 +98,7 @@ def set_test_func(self): raise NotImplementedError("Print test should implement set_test_func") def _run(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(): self.dygraph_func(self.input) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py index 1ddd8e7065f819..337dca6d0d1b9c 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py @@ -22,13 +22,11 @@ import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable from paddle.fluid.optimizer import SGDOptimizer -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static PRINT_STEP = 20 SEED = 2020 -program_translator = ProgramTranslator() class SimpleLSTMRNN(fluid.Layer): @@ -319,12 +317,12 @@ def train(place): def train_dygraph(place): - program_translator.enable(False) + paddle.jit.enable_to_static(False) return train(place) def train_static(place): - program_translator.enable(True) + paddle.jit.enable_to_static(True) return train(place) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py index b28aa1a1c291f8..0218eba16ea0b2 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py @@ -23,7 +23,6 @@ PRINT_STEP = 20 SEED = 2020 -program_translator = paddle.jit.ProgramTranslator() class SimpleLSTMRNN(paddle.nn.Layer): @@ -319,12 +318,12 @@ def train(place): def train_dygraph(place): - program_translator.enable(False) + paddle.jit.enable_to_static(False) return train(place) def train_static(place): - program_translator.enable(True) + paddle.jit.enable_to_static(True) return train(place) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py index 3eeb6f169121b4..2716e54d03fddd 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_reinforcement_learning.py @@ -23,11 +23,9 @@ import paddle.fluid as fluid import paddle.nn.functional as F from paddle.fluid.dygraph import Layer, to_variable -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static SEED = 2020 -program_translator = ProgramTranslator() class Policy(Layer): @@ -61,7 +59,7 @@ class Args: def train(args, place, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) env = gym.make('CartPole-v0') env.seed(SEED) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py index 9e8a854b5b402f..7972904d80cb1e 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet.py @@ -23,7 +23,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.nn import BatchNorm @@ -39,7 +38,6 @@ fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() ) -program_translator = ProgramTranslator() if fluid.is_compiled_with_cuda(): fluid.set_flags({'FLAGS_cudnn_deterministic': True}) @@ -323,7 +321,7 @@ def train(self, to_static, build_strategy=None): return total_loss.numpy() def predict_dygraph(self, data): - program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(place): resnet = ResNet() @@ -382,7 +380,7 @@ def setUp(self): self.resnet_helper = ResNetHelper() def train(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) return self.resnet_helper.train(to_static) def verify_predict(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_amp.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_amp.py index 22c045dc05c503..8b91b4189570cc 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_amp.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_amp.py @@ -20,7 +20,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator # NOTE: Reduce batch_size from 8 to 2 to avoid unittest timeout. batch_size = 2 @@ -29,7 +28,6 @@ fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() ) -program_translator = ProgramTranslator() if fluid.is_compiled_with_cuda(): fluid.set_flags({'FLAGS_cudnn_deterministic': True}) @@ -115,7 +113,7 @@ def train(to_static, build_strategy=None): class TestResnet(unittest.TestCase): def train(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) return train(to_static) def test_resnet(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py index 6cbde55c974fb8..d8d68a30410115 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py @@ -26,7 +26,6 @@ batch_size = 2 epoch_num = 1 -program_translator = ProgramTranslator() if fluid.is_compiled_with_cuda(): fluid.set_flags({'FLAGS_cudnn_deterministic': True}) @@ -114,7 +113,7 @@ def train(to_static, build_strategy=None): class TestResnet(unittest.TestCase): def train(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) build_strategy = paddle.static.BuildStrategy() # Why set `build_strategy.enable_inplace = False` here? # Because we find that this PASS strategy of PE makes dy2st training loss unstable. diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py index 13a267cb3b7a8d..45004d42e2c809 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_v2.py @@ -35,7 +35,6 @@ paddle.CUDAPlace(0) if paddle.is_compiled_with_cuda() else paddle.CPUPlace() ) -program_translator = paddle.jit.ProgramTranslator() if paddle.is_compiled_with_cuda(): paddle.fluid.set_flags({'FLAGS_cudnn_deterministic': True}) @@ -319,7 +318,7 @@ def do_train(self, to_static): return total_loss.numpy() def predict_dygraph(self, data): - program_translator.enable(False) + paddle.jit.enable_to_static(False) paddle.disable_static(place) resnet = ResNet() @@ -380,7 +379,7 @@ def predict_analysis_inference(self, data): return out def train(self, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) return self.do_train(to_static) def verify_predict(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py index b4ec4fb8fd5afe..7ef7c924d5ca84 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py @@ -272,13 +272,12 @@ def setUp(self): else fluid.CPUPlace() ) self.init_dygraph_func() - self.program_translator = ProgramTranslator() def init_dygraph_func(self): self.dygraph_func = test_return_base def _run(self, to_static=False): - self.program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(): res = self.dygraph_func(self.input) if isinstance(res, (tuple, list)): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py index 721e56e4d64f4a..92bc890dcc4d22 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_inference_model.py @@ -20,7 +20,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.jit.dy2static.partial_program import partial_program_from from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX @@ -32,7 +31,6 @@ place = ( fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() ) -program_translator = ProgramTranslator() class SimpleFcLayer(fluid.dygraph.Layer): @@ -148,8 +146,7 @@ def load_and_run_inference( class TestPartialProgramRaiseError(unittest.TestCase): def test_param_type(self): - program_translator = ProgramTranslator() - program_translator.enable(True) + paddle.jit.enable_to_static(True) x_data = np.random.random((20, 20)).astype('float32') with fluid.dygraph.guard(fluid.CPUPlace()): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_load.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_load.py index c2d67514e39871..a7c031536ca19e 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_load.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_save_load.py @@ -22,7 +22,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.optimizer import AdamOptimizer -from paddle.jit import ProgramTranslator np.random.seed(2020) @@ -42,13 +41,12 @@ def tearDown(self): self.temp_dir.cleanup() def test_save_load_same_result(self): - program_translator = ProgramTranslator() x_data = np.random.randn(30, 10, 32).astype('float32') batch_num = 3 with fluid.dygraph.guard(place): - program_translator.enable(True) + paddle.jit.enable_to_static(True) x = fluid.dygraph.to_variable(x_data) net = Linear(32, 64) adam = AdamOptimizer( @@ -81,7 +79,7 @@ def test_save_load_same_result(self): x = fluid.dygraph.to_variable(x_data) # predict output - program_translator.enable(False) + paddle.jit.enable_to_static(False) dygraph_out, dygraph_loss = dygraph_net(x) np.testing.assert_allclose( diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py index 51e6b7068621aa..e01b77af7655b5 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_se_resnet.py @@ -25,7 +25,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph.base import to_variable -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.nn import BatchNorm, Linear @@ -374,8 +373,7 @@ def tearDown(self): self.temp_dir.cleanup() def train(self, train_reader, to_static): - program_translator = ProgramTranslator() - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) np.random.seed(SEED) @@ -473,8 +471,7 @@ def train(self, train_reader, to_static): ) def predict_dygraph(self, data): - program_translator = ProgramTranslator() - program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(place): se_resnext = SeResNeXt() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py index 95cf1365cbc4ab..4cc493bf476428 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_sentiment.py @@ -20,12 +20,10 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import Embedding, Linear SEED = 2020 -program_translator = ProgramTranslator() # Note: Set True to eliminate randomness. # 1. For one operation, cuDNN has several algorithms, @@ -304,7 +302,7 @@ class Args: def train(args, to_static): - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) place = ( fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_seq2seq.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_seq2seq.py index 060909dbc1ac9e..8294b7965f11f5 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_seq2seq.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_seq2seq.py @@ -23,13 +23,11 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.nn import ClipGradByGlobalNorm place = ( fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() ) -program_translator = ProgramTranslator() STEP_NUM = 10 PRINT_STEP = 2 @@ -197,14 +195,14 @@ def tearDown(self): self.temp_dir.cleanup() def run_dygraph(self, mode="train", attn_model=False): - program_translator.enable(False) + paddle.jit.enable_to_static(False) if mode == "train": return train(self.args, attn_model) else: return infer(self.args, attn_model) def run_static(self, mode="train", attn_model=False): - program_translator.enable(True) + paddle.jit.enable_to_static(True) if mode == "train": return train(self.args, attn_model) else: diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py index f5ce0774d29206..e04d411e8d6c90 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py @@ -104,8 +104,7 @@ def train(conf_dict, to_static): """ train process """ - program_translator = ProgramTranslator() - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) # Get device if fluid.is_compiled_with_cuda(): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py index 46afc02f641e78..3e8cb4c10b3d4c 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet_v2.py @@ -102,8 +102,7 @@ def train(conf_dict, to_static): """ train process """ - program_translator = paddle.jit.ProgramTranslator() - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) # Get device if paddle.is_compiled_with_cuda(): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_methods.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_methods.py index 307e8f422de6e6..b1a512c08fddd1 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_methods.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tensor_methods.py @@ -28,8 +28,7 @@ def tensor_clone(x): class TestTensorClone(unittest.TestCase): def _run(self, to_static): - prog_trans = paddle.jit.ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) x = paddle.ones([1, 2, 3]) return tensor_clone(x).numpy() @@ -48,8 +47,7 @@ def tensor_numpy(x): class TestTensorDygraphOnlyMethodError(unittest.TestCase): def _run(self, to_static): - prog_trans = paddle.jit.ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) x = paddle.zeros([2, 2]) y = tensor_numpy(x) return y.numpy() @@ -69,8 +67,7 @@ def tensor_item(x): class TestTensorItem(unittest.TestCase): def _run(self, to_static): - prog_trans = paddle.jit.ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) x = paddle.ones([1]) if to_static: return tensor_item(x).numpy() @@ -92,8 +89,7 @@ def tensor_size(x): class TestTensorSize(unittest.TestCase): def _run(self, to_static): - prog_trans = paddle.jit.ProgramTranslator() - prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) x = paddle.ones([1, 2, 3]) if not to_static: return tensor_size(x) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py index 412b1c9489cb22..eb9e82c2aa7388 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py @@ -25,7 +25,6 @@ import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable -# from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import BatchNorm, Linear diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py index 512103d361cd4c..d61248f6c08a55 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py @@ -21,7 +21,6 @@ import paddle import paddle.fluid as fluid -# from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.nn import Embedding diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_yolov3.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_yolov3.py index d8e76683916066..b84196b421aede 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_yolov3.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_yolov3.py @@ -22,7 +22,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable -from paddle.jit import ProgramTranslator paddle.enable_static() random.seed(0) @@ -78,8 +77,7 @@ def generator(): def train(to_static): - program_translator = ProgramTranslator() - program_translator.enable(to_static) + paddle.jit.enable_to_static(to_static) random.seed(0) np.random.seed(0) diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 8924692ad82e4d..5cf896c394327b 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -1186,8 +1186,7 @@ def func(x): return x_v - prog_trans = paddle.jit.ProgramTranslator() - prog_trans.enable(False) + paddle.jit.enable_to_static(False) x = paddle.ones([1, 2]) # ProgramTranslator is disabled so the func is run in dygraph From cae7c1eb19cc4114792f8cea452dfa025169869b Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 16:20:07 +0800 Subject: [PATCH 09/20] add unitest and fix codestyle bug in api.py --- python/paddle/fluid/framework.py | 2 +- .../dygraph_to_static/test_convert_call.py | 2 -- .../unittests/dygraph_to_static/test_dict.py | 4 ---- .../dygraph_to_static/test_fetch_feed.py | 1 - .../dygraph_to_static/test_for_enumerate.py | 1 - .../dygraph_to_static/test_ifelse.py | 3 --- .../unittests/dygraph_to_static/test_lac.py | 1 - .../dygraph_to_static/test_logical.py | 1 - .../unittests/dygraph_to_static/test_lstm.py | 6 +++--- .../dygraph_to_static/test_mobile_net.py | 1 - .../dygraph_to_static/test_param_guard.py | 8 +++----- .../dygraph_to_static/test_partial_program.py | 1 - .../unittests/dygraph_to_static/test_print.py | 1 - .../test_program_translator.py | 20 +++++++++---------- .../dygraph_to_static/test_ptb_lm.py | 1 - .../dygraph_to_static/test_ptb_lm_v2.py | 1 - .../test_resnet_pure_fp16.py | 1 - .../dygraph_to_static/test_return.py | 2 +- .../dygraph_to_static/test_simnet.py | 1 - .../unittests/dygraph_to_static/test_tsm.py | 1 - .../dygraph_to_static/test_word2vec.py | 1 - python/paddle/jit/api.py | 10 +++++++++- 22 files changed, 27 insertions(+), 43 deletions(-) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 65b823a05ad303..e41ac22ae98bdd 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -564,7 +564,7 @@ def __impl__(*args, **kwargs): raise AssertionError( "'%s' only can be called by `paddle.Tensor` in dynamic graph mode. Suggestions:\n" " 1. If you are in static graph mode, you can switch to dynamic graph mode by turning off `paddle.enable_static()` or calling `paddle.disable_static()`.\n" - " 2. If you are using `@paddle.jit.to_static`, you can turn off ProgramTranslator by calling `paddle.jit.ProgramTranslator().enable(False)`. " + " 2. If you are using `@paddle.jit.to_static`, you can call `paddle.jit.enable_to_static(False)`. " "If you have to translate dynamic graph to static graph, please use other API to replace '%s'." % (func.__name__, func.__name__) ) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py index 83142fc826cb75..c14631c35b6b41 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_convert_call.py @@ -21,10 +21,8 @@ import paddle import paddle.fluid as fluid import paddle.jit.dy2static as _jst -from paddle.jit import ProgramTranslator from paddle.jit.dy2static.convert_call_func import CONVERSION_OPTIONS - SEED = 2020 np.random.seed(SEED) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py index 9ef0af8cc221c3..2a9b8157dea90c 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_dict.py @@ -19,7 +19,6 @@ import paddle import paddle.fluid as fluid from paddle.jit import to_static -from paddle.jit.dy2static.program_translator import ProgramTranslator PLACE = ( fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace() @@ -135,7 +134,6 @@ def _run_dygraph(self): return self.train(to_static=False) def train(self, to_static=False): - prog_trans = ProgramTranslator() paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(PLACE): net = MainNetWithDict(batch_size=self.batch_size) @@ -191,7 +189,6 @@ def _run_dygraph(self): return self._run(to_static=False) def _run(self, to_static): - prog_trans = ProgramTranslator() paddle.jit.enable_to_static(to_static) result = self.dygraph_func(self.input) @@ -237,7 +234,6 @@ def setUp(self): self.x = np.array([2, 2]).astype('float32') def train(self, to_static=False): - prog_trans = ProgramTranslator() paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(PLACE): net = NetWithDictPop() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py index dc49376344967d..b10b96a0329a7d 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_fetch_feed.py @@ -18,7 +18,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static SEED = 2020 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py index bf657e42f61a68..daf6f0a9aca688 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_for_enumerate.py @@ -23,7 +23,6 @@ from paddle.static import InputSpec - # 0. for in range var.numpy()[0] @paddle.jit.to_static def for_in_range(x): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py index 8f3e97b0a8cb02..72ed077af3339e 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py @@ -44,7 +44,6 @@ import paddle import paddle.fluid.core as core import paddle.nn.functional as F -from paddle.jit.dy2static.program_translator import ProgramTranslator from paddle.jit.dy2static.utils import Dygraph2StaticException np.random.seed(1) @@ -254,7 +253,6 @@ def _run_dygraph(self): return self._run(to_static=False) def _run(self, to_static=False): - prog_trans = ProgramTranslator() paddle.jit.enable_to_static(to_static) with fluid.dygraph.guard(place): @@ -364,7 +362,6 @@ def init_net(self): self.Net = DiffModeNet1 def _run(self, mode, to_static): - prog_trans = ProgramTranslator() paddle.jit.enable_to_static(to_static) net = self.Net(mode) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py index 5aeb33bb28a1c4..94e1dba49313a0 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lac.py @@ -27,7 +27,6 @@ from paddle import _legacy_C_ops from paddle.fluid.dygraph import to_variable from paddle.fluid.framework import _non_static_mode -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py index cb4c3ea827294d..2161b2c4edef18 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logical.py @@ -24,7 +24,6 @@ from paddle.jit.dy2static.logical_transformer import cmpop_node_to_str from paddle.utils import gast - SEED = 2020 np.random.seed(22) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lstm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lstm.py index 447488c5ef90ba..1c114a50914cef 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lstm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_lstm.py @@ -52,7 +52,7 @@ def tearDown(self): self.temp_dir.cleanup() def run_lstm(self, to_static): - paddle.jit.ProgramTranslator().enable(to_static) + paddle.jit.enable_to_static(to_static) paddle.disable_static() paddle.static.default_main_program().random_seed = 1001 @@ -70,7 +70,7 @@ def test_lstm_to_static(self): np.testing.assert_allclose(dygraph_out, static_out, rtol=1e-05) def test_save_in_eval(self, with_training=True): - paddle.jit.ProgramTranslator().enable(True) + paddle.jit.enable_to_static(True) net = Net(12, 2) x = paddle.randn((2, 10, 12)) if with_training: @@ -141,7 +141,7 @@ def tearDown(self): self.temp_dir.cleanup() def test_save_in_eval(self): - paddle.jit.ProgramTranslator().enable(True) + paddle.jit.enable_to_static(True) net = LinearNet() x = paddle.randn((2, 10)) x.stop_gradient = False diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py index dfbe8999c1313a..d708dc1eadfedd 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_mobile_net.py @@ -24,7 +24,6 @@ import paddle.fluid as fluid from paddle.fluid.initializer import MSRA from paddle.fluid.param_attr import ParamAttr -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX from paddle.nn import BatchNorm, Linear diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_param_guard.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_param_guard.py index 9270b50b28d339..064d6a1857461b 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_param_guard.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_param_guard.py @@ -17,7 +17,7 @@ import numpy as np import paddle -from paddle.jit import ProgramTranslator, to_static +from paddle.jit import to_static class NetWithParameterList(paddle.nn.Layer): @@ -53,12 +53,11 @@ class TestParameterList(unittest.TestCase): def setUp(self): self.seed = 2021 self.iter_num = 5 - self.prog_trans = ProgramTranslator() def train(self, is_iter, to_static): paddle.seed(self.seed) np.random.seed(self.seed) - self.prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) if is_iter: net = NetWithParameterList(10, 3) else: @@ -110,7 +109,6 @@ class TestRawParameterList(unittest.TestCase): def setUp(self): self.seed = 2021 self.iter_num = 5 - self.prog_trans = ProgramTranslator() def init_net(self): self.net = NetWithRawParamList(10, 3) @@ -118,7 +116,7 @@ def init_net(self): def train(self, to_static): paddle.seed(self.seed) np.random.seed(self.seed) - self.prog_trans.enable(to_static) + paddle.jit.enable_to_static(to_static) self.init_net() sgd = paddle.optimizer.SGD(0.1, parameters=self.net.parameters()) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py index 6d5535848f56a8..36d3ebcc945fa8 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_partial_program.py @@ -20,7 +20,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.layers.utils import flatten -from paddle.jit import ProgramTranslator from paddle.jit.api import to_static SEED = 2020 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py index 913a080243a3a1..5663e1c96f3f58 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_print.py @@ -21,7 +21,6 @@ from paddle.jit import to_static - # 1. print Tensor @to_static def dyfunc_print_variable(x): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py index c4f3ad5b429a70..093eaadb5b96b8 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py @@ -247,19 +247,19 @@ def setUp(self): def test_raise_error(self): with fluid.dygraph.guard(): - self.program_translator.enable(True) + paddle.jit.enable_to_static(True) net = NetWithError() with self.assertRaises(ValueError): net(fluid.dygraph.to_variable(self.x)) def test_enable_disable_get_output(self): - self.program_translator.enable(True) + paddle.jit.enable_to_static(True) with fluid.dygraph.guard(): static_output = self.program_translator.get_output( simple_func, self.x, self.weight ) - self.program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(): dygraph_output = self.program_translator.get_output( simple_func, self.x, self.weight @@ -273,14 +273,14 @@ def test_enable_disable_get_output(self): def test_enable_disable_get_func(self): - self.program_translator.enable(True) + paddle.jit.enable_to_static(True) with fluid.dygraph.guard(): static_func = self.program_translator.get_func(simple_func) self.assertTrue(callable(static_func)) static_output = static_func(self.x, self.weight) self.assertTrue(isinstance(static_output, fluid.Variable)) - self.program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(): dygraph_func = self.program_translator.get_func(simple_func) self.assertTrue(callable(dygraph_func)) @@ -294,7 +294,7 @@ def test_enable_disable_get_func(self): def test_enable_disable_get_program(self): - self.program_translator.enable(True) + paddle.jit.enable_to_static(True) static_output = self.program_translator.get_program( simple_func, self.x, self.weight ) @@ -309,7 +309,7 @@ def test_enable_disable_get_program(self): for var in static_output[3]: self.assertTrue(isinstance(var, fluid.Variable)) - self.program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(): dygraph_output = self.program_translator.get_program( simple_func, self.x, self.weight @@ -323,11 +323,11 @@ def test_enable_disable_get_program(self): def test_enable_disable_declarative(self): - self.program_translator.enable(True) + paddle.jit.enable_to_static(True) with fluid.dygraph.guard(): static_output = decorated_simple_func(self.x, self.weight) - self.program_translator.enable(False) + paddle.jit.enable_to_static(False) with fluid.dygraph.guard(): dygraph_output = decorated_simple_func(self.x, self.weight) np.testing.assert_allclose( @@ -356,7 +356,7 @@ def test_raise_error(self): paddle.enable_static() net = Net() - self.program_translator.enable(True) + paddle.jit.enable_to_static(True) with self.assertRaisesRegex( RuntimeError, "only available in dynamic mode" ): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py index 337dca6d0d1b9c..53687ca6c1ea5b 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm.py @@ -28,7 +28,6 @@ SEED = 2020 - class SimpleLSTMRNN(fluid.Layer): def __init__( self, hidden_size, num_steps, num_layers=2, init_scale=0.1, dropout=None diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py index 0218eba16ea0b2..b065a51bd56d81 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ptb_lm_v2.py @@ -24,7 +24,6 @@ SEED = 2020 - class SimpleLSTMRNN(paddle.nn.Layer): def __init__( self, hidden_size, num_steps, num_layers=2, init_scale=0.1, dropout=None diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py index d8d68a30410115..1f9c1d1104696d 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_resnet_pure_fp16.py @@ -20,7 +20,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator # NOTE: Reduce batch_size from 8 to 2 to avoid unittest timeout. batch_size = 2 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py index 7ef7c924d5ca84..48c60795c4c38b 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py @@ -20,7 +20,7 @@ import paddle import paddle.fluid as fluid import paddle.fluid.core as core -from paddle.jit import ProgramTranslator, to_static +from paddle.jit import to_static from paddle.jit.dy2static.utils import Dygraph2StaticException SEED = 2020 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py index e04d411e8d6c90..83512ba8826fcf 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_simnet.py @@ -21,7 +21,6 @@ import paddle import paddle.fluid as fluid -from paddle.jit import ProgramTranslator SEED = 102 random.seed(SEED) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py index eb9e82c2aa7388..66e882e477e741 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_tsm.py @@ -24,7 +24,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.dygraph import to_variable - from paddle.jit.api import to_static from paddle.nn import BatchNorm, Linear diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py index d61248f6c08a55..560132565907e5 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_word2vec.py @@ -20,7 +20,6 @@ import paddle import paddle.fluid as fluid - from paddle.jit.api import to_static from paddle.nn import Embedding diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index c17567b10221ab..b077359e6d8c52 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -75,7 +75,15 @@ from paddle.fluid.framework import dygraph_only, _non_static_mode from paddle.fluid.wrapped_decorator import wrap_decorator -__all__ = [] +__all__ = [ + "save", + "load", + "to_static", + "not_to_static", + "ProgramTranslator", # TODO(RyanHuang): Remove it + "TranslatedLayer", + "enable_to_static", +] def create_program_from_desc(program_desc): From b447b4580f36fdc3964c3106796942111ebf6277 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 19:00:33 +0800 Subject: [PATCH 10/20] finish all unitest --- .../unittests/dygraph_to_static/test_error.py | 24 - .../dygraph_to_static/test_full_name_usage.py | 9 - .../test_program_translator.py | 446 ------------------ .../unittests/test_directory_migration.py | 2 - 4 files changed, 481 deletions(-) delete mode 100644 python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py index ac91f9a74efac4..9a41b24261f104 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py @@ -174,7 +174,6 @@ def setUp(self): self.filepath = inspect.getfile(unwrap(self.func_call)) self.set_exception_type() self.set_message() - self.prog_trans = paddle.jit.ProgramTranslator() def set_input(self): self.input = np.ones([3, 2]) @@ -364,29 +363,6 @@ def set_message(self): ] -# Situation 2: Call ProgramTranslator().get_output(...) to use Dynamic-to-Static -class TestErrorGetOutputInCompiletime(TestErrorStaticLayerCallInCompiletime): - def set_func_call(self): - self.func_call = lambda: self.prog_trans.get_output( - unwrap(self.func), self.input - ) - - -class TestErrorGetOutputInCompiletime_2( - TestErrorStaticLayerCallInCompiletime_2 -): - def set_func_call(self): - self.func_call = lambda: self.prog_trans.get_output( - unwrap(self.func), self.input - ) - - -class TestErrorGetOutputInRuntime(TestErrorStaticLayerCallInRuntime): - def set_func_call(self): - self.func_call = lambda: self.prog_trans.get_output( - unwrap(self.func), self.input - ) - class TestJitSaveInCompiletime(TestErrorBase): def setUp(self): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_full_name_usage.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_full_name_usage.py index 0777279942ee0e..a33263faa0b400 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_full_name_usage.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_full_name_usage.py @@ -78,14 +78,5 @@ def test_run_success(self): DoubleDecorated().double_decorated_func2(x) -class TestImportProgramTranslator(unittest.TestCase): - def test_diff_pkg_same_cls(self): - dygraph_prog_trans = paddle.jit.ProgramTranslator() - dy_to_stat_prog_trans = paddle.jit.ProgramTranslator() - full_pkg_prog_trans = paddle.jit.ProgramTranslator() - self.assertEqual(dygraph_prog_trans, dy_to_stat_prog_trans) - self.assertEqual(dygraph_prog_trans, full_pkg_prog_trans) - - if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py deleted file mode 100644 index 093eaadb5b96b8..00000000000000 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py +++ /dev/null @@ -1,446 +0,0 @@ -# Copyright (c) 2020 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 inspect -import textwrap -import unittest - -import astor -import numpy as np -from ifelse_simple_func import ( - dyfunc_with_if_else, - dyfunc_with_if_else_early_return1, - dyfunc_with_if_else_early_return2, -) - -import paddle -import paddle.fluid as fluid -import paddle.jit.dy2static as _jst -from paddle.jit import ProgramTranslator -from paddle.jit.api import to_static -from paddle.jit.dy2static.utils import func_to_source_code -from paddle.utils import gast - -np.random.seed(0) - - -# TODO(Aurelius): Currently, `declarative` don't support decorate the function -# that contains layers with initialized operation, like `fc = linear(10, 3)`. -# Because initialized ops will be added into program and be executed many times. -# The parameters are assumed to initialized outside of the function. -def simple_func(x, weight_numpy): - x = fluid.dygraph.to_variable(x) - w = fluid.dygraph.to_variable(weight_numpy) - y = paddle.matmul(x, w) - z = paddle.mean(y) - return z - - -@to_static -def decorated_simple_func(x, weight_numpy): - x = fluid.dygraph.to_variable(x) - w = fluid.dygraph.to_variable(weight_numpy) - y = paddle.matmul(x, w) - z = paddle.mean(y) - return z - - -def get_source_code(func): - raw_code = inspect.getsource(func) - code = textwrap.dedent(raw_code) - root = gast.parse(code) - source_code = astor.to_source(gast.gast_to_ast(root)) - return source_code - - -class StaticCode1: - def dyfunc_with_if_else(x_v, label=None): - loss = _jst.UndefinedVar('loss') - __return_1 = _jst.UndefinedVar('__return_1') - __return_0 = _jst.UndefinedVar('__return_0') - __return_value_0 = None - - def get_args_0(): - nonlocal x_v - return (x_v,) - - def set_args_0(__args): - nonlocal x_v - (x_v,) = __args - - def true_fn_0(): - nonlocal x_v - x_v = x_v - 1 - return - - def false_fn_0(): - nonlocal x_v - x_v = x_v + 1 - return - - _jst.IfElse( - paddle.mean(x_v)[0] > 5, - true_fn_0, - false_fn_0, - get_args_0, - set_args_0, - ('x_v',), - push_pop_names=None, - ) - - def get_args_1(): - nonlocal __return_0, __return_1, __return_value_0, loss - return __return_0, __return_1, __return_value_0, loss - - def set_args_1(__args): - nonlocal __return_0, __return_1, __return_value_0, loss - __return_0, __return_1, __return_value_0, loss = __args - - def true_fn_1(): - nonlocal __return_0, __return_1, __return_value_0, loss - loss = paddle.nn.functional.cross_entropy( - x_v, label, reduction='none', use_softmax=False - ) - __return_0 = _jst.create_bool_as_type(label is not None, True) - __return_value_0 = loss - return - - def false_fn_1(): - nonlocal __return_0, __return_1, __return_value_0, loss - __return_1 = _jst.create_bool_as_type(label is not None, True) - __return_value_0 = x_v - return - - _jst.IfElse( - label is not None, - true_fn_1, - false_fn_1, - get_args_1, - set_args_1, - ('__return_0', '__return_1', '__return_value_0', 'loss'), - push_pop_names=None, - ) - return __return_value_0 - - -class StaticCode2: - # TODO: Transform return statement - def dyfunc_with_if_else(x_v, label=None): - loss = _jst.UndefinedVar('loss') - __return_3 = _jst.UndefinedVar('__return_3') - __return_2 = _jst.UndefinedVar('__return_2') - __return_value_1 = None - - def get_args_2(): - nonlocal x_v - return (x_v,) - - def set_args_2(__args): - nonlocal x_v - (x_v,) = __args - - def true_fn_2(): - nonlocal x_v - x_v = x_v - 1 - return - - def false_fn_2(): - nonlocal x_v - x_v = x_v + 1 - return - - _jst.IfElse( - paddle.mean(x_v)[0] > 5, - true_fn_2, - false_fn_2, - get_args_2, - set_args_2, - ('x_v',), - push_pop_names=None, - ) - - def get_args_3(): - nonlocal __return_2, __return_3, __return_value_1, loss - return __return_2, __return_3, __return_value_1, loss - - def set_args_3(__args): - nonlocal __return_2, __return_3, __return_value_1, loss - __return_2, __return_3, __return_value_1, loss = __args - - def true_fn_3(): - nonlocal __return_2, __return_3, __return_value_1, loss - loss = paddle.nn.functional.cross_entropy( - x_v, label, reduction='none', use_softmax=False - ) - __return_2 = _jst.create_bool_as_type(label is not None, True) - __return_value_1 = loss - return - - def false_fn_3(): - nonlocal __return_2, __return_3, __return_value_1, loss - __return_3 = _jst.create_bool_as_type(label is not None, True) - __return_value_1 = x_v - return - - _jst.IfElse( - label is not None, - true_fn_3, - false_fn_3, - get_args_3, - set_args_3, - ('__return_2', '__return_3', '__return_value_1', 'loss'), - push_pop_names=None, - ) - return __return_value_1 - - -class NetWithError(fluid.dygraph.layers.Layer): - @to_static - def forward(self, x): - linear = paddle.nn.Linear(32, 64) - y = linear(x) - return y - - -class TestDygraphToStaticCode(unittest.TestCase): - def setUp(self): - # set to print all string diff when assertEqual fails - self.maxDiff = None - - def test_decorator(self): - program_translator = ProgramTranslator() - code = program_translator.get_code(dyfunc_with_if_else) - print(code) - answer = get_source_code(StaticCode1.dyfunc_with_if_else) - self.assertEqual( - answer.replace('\n', '').replace(' ', ''), - code.replace('\n', '').replace(' ', ''), - ) - - def test_program_translator(self): - answer = get_source_code(StaticCode2.dyfunc_with_if_else) - program_translator = ProgramTranslator() - code = program_translator.get_code(dyfunc_with_if_else) - print(code) - self.assertEqual( - answer.replace('\n', '').replace(' ', ''), - code.replace('\n', '').replace(' ', ''), - ) - - -class TestEnableDeclarative(unittest.TestCase): - def setUp(self): - self.x = np.random.randn(30, 10, 32).astype('float32') - self.weight = np.random.randn(32, 64).astype('float32') - self.program_translator = ProgramTranslator() - - def test_raise_error(self): - with fluid.dygraph.guard(): - paddle.jit.enable_to_static(True) - net = NetWithError() - with self.assertRaises(ValueError): - net(fluid.dygraph.to_variable(self.x)) - - def test_enable_disable_get_output(self): - paddle.jit.enable_to_static(True) - with fluid.dygraph.guard(): - static_output = self.program_translator.get_output( - simple_func, self.x, self.weight - ) - - paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(): - dygraph_output = self.program_translator.get_output( - simple_func, self.x, self.weight - ) - np.testing.assert_allclose( - static_output.numpy(), - dygraph_output.numpy(), - rtol=1e-05, - atol=1e-4, - ) - - def test_enable_disable_get_func(self): - - paddle.jit.enable_to_static(True) - with fluid.dygraph.guard(): - static_func = self.program_translator.get_func(simple_func) - self.assertTrue(callable(static_func)) - static_output = static_func(self.x, self.weight) - self.assertTrue(isinstance(static_output, fluid.Variable)) - - paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(): - dygraph_func = self.program_translator.get_func(simple_func) - self.assertTrue(callable(dygraph_func)) - dygraph_output = dygraph_func(self.x, self.weight) - self.assertTrue( - isinstance( - dygraph_output, - (fluid.core.VarBase, fluid.core.eager.Tensor), - ) - ) - - def test_enable_disable_get_program(self): - - paddle.jit.enable_to_static(True) - static_output = self.program_translator.get_program( - simple_func, self.x, self.weight - ) - self.assertTrue(isinstance(static_output, tuple)) - self.assertEqual(len(static_output), 4) - self.assertTrue(isinstance(static_output[0], fluid.Program)) - self.assertTrue(isinstance(static_output[1], fluid.Program)) - # Check all inputs and outputs are Variable - for var in static_output[2]: - self.assertTrue(isinstance(var, fluid.Variable)) - - for var in static_output[3]: - self.assertTrue(isinstance(var, fluid.Variable)) - - paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(): - dygraph_output = self.program_translator.get_program( - simple_func, self.x, self.weight - ) - self.assertTrue( - isinstance( - dygraph_output, - (fluid.core.VarBase, fluid.core.eager.Tensor), - ) - ) - - def test_enable_disable_declarative(self): - - paddle.jit.enable_to_static(True) - with fluid.dygraph.guard(): - static_output = decorated_simple_func(self.x, self.weight) - - paddle.jit.enable_to_static(False) - with fluid.dygraph.guard(): - dygraph_output = decorated_simple_func(self.x, self.weight) - np.testing.assert_allclose( - static_output.numpy(), - dygraph_output.numpy(), - rtol=1e-05, - atol=1e-4, - ) - - -class Net(fluid.dygraph.layers.Layer): - def __init__(self): - super().__init__() - - def forward(self, x): - return x + 1 - - -class TestErrorWithInitFromStaticMode(unittest.TestCase): - def setUp(self): - self.program_translator = ProgramTranslator() - self.x = np.random.randn(10, 32).astype('float32') - - def test_raise_error(self): - # disable imperative - paddle.enable_static() - net = Net() - - paddle.jit.enable_to_static(True) - with self.assertRaisesRegex( - RuntimeError, "only available in dynamic mode" - ): - self.program_translator.get_output(net.forward, self.x) - - with self.assertRaisesRegex( - RuntimeError, "only available in dynamic mode" - ): - self.program_translator.get_program(net.forward, self.x) - - -class SwitchModeNet(paddle.nn.Layer): - def __init__(self): - super().__init__() - - @paddle.jit.to_static - def forward(self, x): - return x + 1 - - @paddle.jit.to_static - def foo(self): - return True - - -@paddle.jit.to_static -def switch_mode_funciton(): - return True - - -class TestFunctionTrainEvalMode(unittest.TestCase): - def test_switch_mode(self): - paddle.disable_static() - switch_mode_funciton.eval() - switch_mode_funciton() - self.assertEqual(switch_mode_funciton._training, False) - _, partial_layer = switch_mode_funciton.program_cache.last()[-1] - self.assertEqual(partial_layer.training, False) - - switch_mode_funciton.train() - switch_mode_funciton() - self.assertEqual(switch_mode_funciton._training, True) - _, partial_layer = switch_mode_funciton.program_cache.last()[-1] - self.assertEqual(partial_layer.training, True) - - def test_raise_error(self): - paddle.disable_static() - net = SwitchModeNet() - - self.assertEqual(net.training, True) - with self.assertRaises(RuntimeError): - net.forward.eval() - - net.eval() - self.assertEqual(net.training, False) - with self.assertRaises(RuntimeError): - net.foo.train() - - -class TestIfElseEarlyReturn(unittest.TestCase): - def test_ifelse_early_return1(self): - answer = np.zeros([2, 2]) + 1 - static_func = paddle.jit.to_static(dyfunc_with_if_else_early_return1) - out = static_func() - np.testing.assert_allclose(answer, out[0].numpy(), rtol=1e-05) - - def test_ifelse_early_return2(self): - answer = np.zeros([2, 2]) + 3 - static_func = paddle.jit.to_static(dyfunc_with_if_else_early_return2) - out = static_func() - np.testing.assert_allclose(answer, out[0].numpy(), rtol=1e-05) - - -class TestRemoveCommentInDy2St(unittest.TestCase): - def func_with_comment(self): - # Comment1 - x = paddle.to_tensor([1, 2, 3]) - # Comment2 - # Comment3 - y = paddle.to_tensor([4, 5, 6]) - - def test_remove_comment(self): - code_string = func_to_source_code(self.func_with_comment) - self.assertEqual('#' not in code_string, True) - - -if __name__ == '__main__': - unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_directory_migration.py b/python/paddle/fluid/tests/unittests/test_directory_migration.py index 439104b69a18c4..948db941b183c3 100644 --- a/python/paddle/fluid/tests/unittests/test_directory_migration.py +++ b/python/paddle/fluid/tests/unittests/test_directory_migration.py @@ -49,7 +49,6 @@ def test_new_directory(self): 'paddle.DataParallel', 'paddle.jit', 'paddle.jit.to_static', - 'paddle.jit.ProgramTranslator', 'paddle.jit.TranslatedLayer', 'paddle.jit.save', 'paddle.jit.load', @@ -143,7 +142,6 @@ def test_old_directory(self): 'paddle.imperative.jit', 'paddle.imperative.TracedLayer', 'paddle.imperative.declarative', - 'paddle.imperative.ProgramTranslator', 'paddle.imperative.TranslatedLayer', 'paddle.imperative.jit.save', 'paddle.imperative.jit.load', From 53cd9f16b81b5c9402edcd07cc3552783e85f264 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 19:16:48 +0800 Subject: [PATCH 11/20] remove ProgramTranslator --- python/paddle/jit/__init__.py | 6 - python/paddle/jit/api.py | 5 +- .../jit/dy2static/program_translator.py | 280 +----------------- 3 files changed, 4 insertions(+), 287 deletions(-) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index f218419a867dad..4e6c4829b99bcc 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -19,18 +19,12 @@ from .api import not_to_static from .api import enable_to_static from .dy2static.logging_utils import set_code_level, set_verbosity - -from . import dy2static -from .dy2static.program_translator import ( - ProgramTranslator, -) # TODO(RyanHuang): Remove it from .translated_layer import TranslatedLayer __all__ = [ # noqa 'save', 'load', 'to_static', - 'ProgramTranslator', # TODO(RyanHuang): Remove it 'TranslatedLayer', 'set_code_level', 'set_verbosity', diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index b077359e6d8c52..c4f2e80a2176dd 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -80,7 +80,6 @@ "load", "to_static", "not_to_static", - "ProgramTranslator", # TODO(RyanHuang): Remove it "TranslatedLayer", "enable_to_static", ] @@ -168,7 +167,7 @@ def __impl__(*args, **kwargs): if _non_static_mode() or not program_translator.enable_to_static: logging_utils.warn( "The decorator 'dygraph_to_static_func' doesn't work in " - "dygraph mode or set ProgramTranslator.enable to False. " + "dygraph mode or set 'paddle.jit.enable_to_static' to False. " "We will just return dygraph output." ) return dygraph_func(*args, **kwargs) @@ -891,7 +890,7 @@ def fun(inputs): prog_translator = ProgramTranslator() if not prog_translator.enable_to_static: raise RuntimeError( - "The paddle.jit.save doesn't work when setting ProgramTranslator.enable to False." + "The paddle.jit.save doesn't work when setting 'paddle.jit.enable_to_static' to False." ) if not ( diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 5cf896c394327b..111c85a43f0559 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -405,9 +405,9 @@ def __call__(self, *args, **kwargs): # will show up **only once**. StaticFunction.__call__ will run many times, it is appropriate to # display this warning message only once. logging_utils.warn( - "The decorator '@paddle.jit.to_static' does NOT work when setting ProgramTranslator.enable to False. " + "The decorator '@paddle.jit.to_static' does NOT work when setting 'paddle.jit.enable_to_static' to False. " "We will just return dygraph output. If you would like to get static graph output, please call API " - "ProgramTranslator.enable(True)" + "paddle.jit.enable_to_static(True)" ) return self._call_dygraph_function(*args, **kwargs) @@ -1201,282 +1201,6 @@ def func(x): ) self.enable_to_static = enable_to_static - def get_output(self, dygraph_func, *args, **kwargs): - """ - Returns the output dygraph Tensor for dygraph function. The dygraph - function will be translated into static graph function so the under - beneath numerical result will be calculated by static graph mode. - - Args: - dygraph_func (callable): the dygraph function. - *args (tuple): the input argument of dygraph_func. - **kwargs (dict): the input argument of dygraph_func. - - Returns: - Tensor or tuple of Tensors: the dygraph Tensor containing digital result. - - Examples: - .. code-block:: python - - import paddle - - - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v - - - prog_trans = paddle.jit.ProgramTranslator() - - x = paddle.ones([1, 2]) - x_v = prog_trans.get_output(func, x) - print(x_v) # [[0. 0.]] - - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_output" - - if not self.enable_to_static: - # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) - # will show up **only once**. - logging_utils.warn( - "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. " - "We will just return dygraph output. " - "Please call ProgramTranslator.enable(True) if you would like to get static output." - ) - return dygraph_func(*args, **kwargs) - try: - function_spec = FunctionSpec(dygraph_func) - cache_key = CacheKey.from_func_and_args( - function_spec, - args, - kwargs, - getattr(dygraph_func, '__self__', None), - ) - _, partial_program_layer = self._program_cache[cache_key] - - if args and isinstance(args[0], layers.Layer): - # Synchronize self.training attribute. - partial_program_layer.training = args[0].training - args = args[1:] - try: - return partial_program_layer(args) - except BaseException as e: - # NOTE: - # 1. If e is raised in compile time, e should have been attached to ERROR_DATA before; - # 2. If e raised in runtime, e should be attached to ERROR_DATA here. - if not hasattr(e, error.ERROR_DATA): - # runtime error - error.attach_error_data(e, in_runtime=True) - raise - except BaseException as e: - error_data = getattr(e, error.ERROR_DATA, None) - if error_data: - error_data.raise_new_exception() - else: - logging_utils.warn( - "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'" - " if you can't handle this {} yourself.".format(type(e)) - ) - raise e - - def get_func(self, dygraph_func): - """ - Returns a callable function which converts imperative dygraph APIs of - the input dygraph_func into declarative net-building APIs, which means - it doesn't return immediate digital result as get_output does. - Users should handle Program and Executor by themselves. - - Args: - dygraph_func (callable): the dygraph function. - - Returns: - callable: converting imperative dygraph APIs into declarative - net-building APIs. - - Examples: - .. code-block:: python - - import paddle - - - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v - - - prog_trans = paddle.jit.ProgramTranslator() - static_func = prog_trans.get_func(func) - print(callable(static_func)) # True - - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_func" - - if not self.enable_to_static: - logging_utils.warn( - "The ProgramTranslator.get_func doesn't work when setting ProgramTranslator.enable to False. We will " - "just return dygraph output. Please call ProgramTranslator.enable(True) if you would like to get static output." - ) - return dygraph_func - - static_func = convert_to_static(dygraph_func) - return static_func - - def get_program(self, dygraph_func, *args, **kwargs): - """ - Returns the translated static program and input/output Tensors from - dygraph function. The users can use the program to run by executor. - - Args: - dygraph_func (callable): the dygraph function. - *args (tuple): the input argument of dygraph_func. - **kwargs (dict): the input argument of dygraph_func. - - Returns: - tuple of (main_program, startup_program, inputs, outputs) whose - types are (Program, Program, list of Tensors, list of Tensors). - main_program: the converted main program. - startup_program: the converted startup program. - inputs: list of input Tensors which need to be fed. - outputs: list of output Tensors which users can fetch. - - Examples: - .. code-block:: python - - import paddle - - - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v - - - prog_trans = paddle.jit.ProgramTranslator() - x = paddle.ones([1, 2]) - main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) - print([i.name for i in inputs]) - # [u'generated_tensor_0'] the feed input Tensor name representing x - print([o.name for o in outputs]) - # [u'_generated_var_4'] the fetch output Tensor name representing x_v - - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_program" - - if not self.enable_to_static: - logging_utils.warn( - "The ProgramTranslator.get_program doesn't work when setting ProgramTranslator.enable to False." - "We will just return dygraph output. " - "Please call ProgramTranslator.enable(True) if you would like to get static output." - ) - return dygraph_func(*args, **kwargs) - - function_spec = FunctionSpec(dygraph_func) - cache_key = CacheKey.from_func_and_args( - function_spec, args, kwargs, getattr(dygraph_func, '__self__', None) - ) - concrete_program, partial_program_layer = self._program_cache[cache_key] - - # Note: concrete_program hold all input/output infos include non-Variable - input_vars = [ - var - for var in concrete_program.inputs - if isinstance(var, framework.Variable) - ] - output_vars = [ - var - for var in concrete_program.outputs - if isinstance(var, framework.Variable) - ] - - return ( - concrete_program.main_program, - concrete_program.startup_program, - input_vars, - output_vars, - ) - - def get_code(self, dygraph_func): - """ - Returns the translated static function string code from dygraph function. - - Args: - dygraph_func (callable): the dygraph function. - - Returns: - str: the string code of translated static function. - - Examples: - .. code-block:: python - - import paddle - - - def func(x): - if paddle.mean(x) > 0: - x_v = x - 1 - else: - x_v = x + 1 - return x_v - - - prog_trans = paddle.jit.ProgramTranslator() - - code = prog_trans.get_code(func) - print(type(code)) # - - """ - assert callable( - dygraph_func - ), "Input dygraph_func is not a callable in ProgramTranslator.get_code" - # Gets AST from dygraph function - - unwrap_func = unwrap(dygraph_func) - raw_code = inspect.getsource(unwrap_func) - code = textwrap.dedent(raw_code) - root = gast.parse(code) - - # Transform AST - dygraph_to_static = DygraphToStaticAst() - root_wrapper = dygraph_to_static.get_static_ast(root) - - # Get source_code - source_code = ast_to_source_code(root_wrapper.node) - return source_code - - def get_program_cache(self): - """ - Returns the ProgramCache instance. This method is used by PaddlePaddle - developers to manage program cache in ProgramTranslator. Normal users - don't have to call this method. - - Returns: - ProgramCache: ProgramCache instance of ProgramTranslator. - - Examples: - .. code-block:: python - - import paddle - - prog_trans = paddle.jit.ProgramTranslator() - prog_cache = prog_trans.get_program_cache() - - """ - return self._program_cache - def enable_to_static(enable_to_static_bool): From 181f7e7f38b6cd5d81f1cef0257cb3729d2e85ba Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 19:25:08 +0800 Subject: [PATCH 12/20] fix code style --- .../fluid/tests/unittests/dygraph_to_static/test_error.py | 1 - python/paddle/jit/dy2static/program_translator.py | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py index 9a41b24261f104..13af9b4d6b1513 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py @@ -363,7 +363,6 @@ def set_message(self): ] - class TestJitSaveInCompiletime(TestErrorBase): def setUp(self): self.reset_flags_to_default() diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 111c85a43f0559..1f686d38201f27 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -14,7 +14,6 @@ import collections import inspect -import textwrap import threading import weakref @@ -25,7 +24,7 @@ from paddle.fluid.layers.utils import flatten from paddle.utils import gast -from . import ast_to_source_code, error, logging_utils +from . import error, logging_utils from .ast_transformer import DygraphToStaticAst from .function_spec import ( FunctionSpec, From d2d9bfd74aba4d6675b2b5c962754e7c71e7efc3 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 20:29:27 +0800 Subject: [PATCH 13/20] restore test_program_translator --- .../test_program_translator.py | 353 ++++++++++++++++++ 1 file changed, 353 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py new file mode 100644 index 00000000000000..a1326cd7b8174e --- /dev/null +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py @@ -0,0 +1,353 @@ +# Copyright (c) 2020 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 inspect +import textwrap +import unittest + +import astor +import numpy as np +from ifelse_simple_func import ( + dyfunc_with_if_else, + dyfunc_with_if_else_early_return1, + dyfunc_with_if_else_early_return2, +) + +import paddle +import paddle.fluid as fluid +import paddle.jit.dy2static as _jst +from paddle.jit.api import to_static +from paddle.jit.dy2static.program_translator import ProgramTranslator +from paddle.jit.dy2static.utils import func_to_source_code +from paddle.utils import gast + +np.random.seed(0) + + +# TODO(Aurelius): Currently, `declarative` don't support decorate the function +# that contains layers with initialized operation, like `fc = linear(10, 3)`. +# Because initialized ops will be added into program and be executed many times. +# The parameters are assumed to initialized outside of the function. +def simple_func(x, weight_numpy): + x = fluid.dygraph.to_variable(x) + w = fluid.dygraph.to_variable(weight_numpy) + y = paddle.matmul(x, w) + z = paddle.mean(y) + return z + + +@to_static +def decorated_simple_func(x, weight_numpy): + x = fluid.dygraph.to_variable(x) + w = fluid.dygraph.to_variable(weight_numpy) + y = paddle.matmul(x, w) + z = paddle.mean(y) + return z + + +def get_source_code(func): + raw_code = inspect.getsource(func) + code = textwrap.dedent(raw_code) + root = gast.parse(code) + source_code = astor.to_source(gast.gast_to_ast(root)) + return source_code + + +class StaticCode1: + def dyfunc_with_if_else(x_v, label=None): + loss = _jst.UndefinedVar('loss') + __return_1 = _jst.UndefinedVar('__return_1') + __return_0 = _jst.UndefinedVar('__return_0') + __return_value_0 = None + + def get_args_0(): + nonlocal x_v + return (x_v,) + + def set_args_0(__args): + nonlocal x_v + (x_v,) = __args + + def true_fn_0(): + nonlocal x_v + x_v = x_v - 1 + return + + def false_fn_0(): + nonlocal x_v + x_v = x_v + 1 + return + + _jst.IfElse( + paddle.mean(x_v)[0] > 5, + true_fn_0, + false_fn_0, + get_args_0, + set_args_0, + ('x_v',), + push_pop_names=None, + ) + + def get_args_1(): + nonlocal __return_0, __return_1, __return_value_0, loss + return __return_0, __return_1, __return_value_0, loss + + def set_args_1(__args): + nonlocal __return_0, __return_1, __return_value_0, loss + __return_0, __return_1, __return_value_0, loss = __args + + def true_fn_1(): + nonlocal __return_0, __return_1, __return_value_0, loss + loss = paddle.nn.functional.cross_entropy( + x_v, label, reduction='none', use_softmax=False + ) + __return_0 = _jst.create_bool_as_type(label is not None, True) + __return_value_0 = loss + return + + def false_fn_1(): + nonlocal __return_0, __return_1, __return_value_0, loss + __return_1 = _jst.create_bool_as_type(label is not None, True) + __return_value_0 = x_v + return + + _jst.IfElse( + label is not None, + true_fn_1, + false_fn_1, + get_args_1, + set_args_1, + ('__return_0', '__return_1', '__return_value_0', 'loss'), + push_pop_names=None, + ) + return __return_value_0 + + +class StaticCode2: + # TODO: Transform return statement + def dyfunc_with_if_else(x_v, label=None): + loss = _jst.UndefinedVar('loss') + __return_3 = _jst.UndefinedVar('__return_3') + __return_2 = _jst.UndefinedVar('__return_2') + __return_value_1 = None + + def get_args_2(): + nonlocal x_v + return (x_v,) + + def set_args_2(__args): + nonlocal x_v + (x_v,) = __args + + def true_fn_2(): + nonlocal x_v + x_v = x_v - 1 + return + + def false_fn_2(): + nonlocal x_v + x_v = x_v + 1 + return + + _jst.IfElse( + paddle.mean(x_v)[0] > 5, + true_fn_2, + false_fn_2, + get_args_2, + set_args_2, + ('x_v',), + push_pop_names=None, + ) + + def get_args_3(): + nonlocal __return_2, __return_3, __return_value_1, loss + return __return_2, __return_3, __return_value_1, loss + + def set_args_3(__args): + nonlocal __return_2, __return_3, __return_value_1, loss + __return_2, __return_3, __return_value_1, loss = __args + + def true_fn_3(): + nonlocal __return_2, __return_3, __return_value_1, loss + loss = paddle.nn.functional.cross_entropy( + x_v, label, reduction='none', use_softmax=False + ) + __return_2 = _jst.create_bool_as_type(label is not None, True) + __return_value_1 = loss + return + + def false_fn_3(): + nonlocal __return_2, __return_3, __return_value_1, loss + __return_3 = _jst.create_bool_as_type(label is not None, True) + __return_value_1 = x_v + return + + _jst.IfElse( + label is not None, + true_fn_3, + false_fn_3, + get_args_3, + set_args_3, + ('__return_2', '__return_3', '__return_value_1', 'loss'), + push_pop_names=None, + ) + return __return_value_1 + + +class NetWithError(fluid.dygraph.layers.Layer): + @to_static + def forward(self, x): + linear = paddle.nn.Linear(32, 64) + y = linear(x) + return y + + +class TestDygraphToStaticCode(unittest.TestCase): + def setUp(self): + # set to print all string diff when assertEqual fails + self.maxDiff = None + + def test_decorator(self): + code = paddle.jit.to_static(dyfunc_with_if_else).code + print(code) + answer = get_source_code(StaticCode1.dyfunc_with_if_else) + self.assertEqual( + answer.replace('\n', '').replace(' ', ''), + code.replace('\n', '').replace(' ', ''), + ) + + def test_program_translator(self): + answer = get_source_code(StaticCode2.dyfunc_with_if_else) + code = paddle.jit.to_static(dyfunc_with_if_else).code + print(code) + self.assertEqual( + answer.replace('\n', '').replace(' ', ''), + code.replace('\n', '').replace(' ', ''), + ) + + +class TestEnableDeclarative(unittest.TestCase): + def setUp(self): + self.x = np.random.randn(30, 10, 32).astype('float32') + self.weight = np.random.randn(32, 64).astype('float32') + self.program_translator = ProgramTranslator() + + def test_raise_error(self): + with fluid.dygraph.guard(): + paddle.jit.enable_to_static(True) + net = NetWithError() + with self.assertRaises(ValueError): + net(fluid.dygraph.to_variable(self.x)) + + def test_enable_disable_declarative(self): + + paddle.jit.enable_to_static(True) + with fluid.dygraph.guard(): + static_output = decorated_simple_func(self.x, self.weight) + + paddle.jit.enable_to_static(False) + with fluid.dygraph.guard(): + dygraph_output = decorated_simple_func(self.x, self.weight) + np.testing.assert_allclose( + static_output.numpy(), + dygraph_output.numpy(), + rtol=1e-05, + atol=1e-4, + ) + + +class Net(fluid.dygraph.layers.Layer): + def __init__(self): + super().__init__() + + def forward(self, x): + return x + 1 + + +class SwitchModeNet(paddle.nn.Layer): + def __init__(self): + super().__init__() + + @paddle.jit.to_static + def forward(self, x): + return x + 1 + + @paddle.jit.to_static + def foo(self): + return True + + +@paddle.jit.to_static +def switch_mode_funciton(): + return True + + +class TestFunctionTrainEvalMode(unittest.TestCase): + def test_switch_mode(self): + paddle.disable_static() + switch_mode_funciton.eval() + switch_mode_funciton() + self.assertEqual(switch_mode_funciton._training, False) + _, partial_layer = switch_mode_funciton.program_cache.last()[-1] + self.assertEqual(partial_layer.training, False) + + switch_mode_funciton.train() + switch_mode_funciton() + self.assertEqual(switch_mode_funciton._training, True) + _, partial_layer = switch_mode_funciton.program_cache.last()[-1] + self.assertEqual(partial_layer.training, True) + + def test_raise_error(self): + paddle.disable_static() + net = SwitchModeNet() + + self.assertEqual(net.training, True) + with self.assertRaises(RuntimeError): + net.forward.eval() + + net.eval() + self.assertEqual(net.training, False) + with self.assertRaises(RuntimeError): + net.foo.train() + + +class TestIfElseEarlyReturn(unittest.TestCase): + def test_ifelse_early_return1(self): + answer = np.zeros([2, 2]) + 1 + static_func = paddle.jit.to_static(dyfunc_with_if_else_early_return1) + out = static_func() + np.testing.assert_allclose(answer, out[0].numpy(), rtol=1e-05) + + def test_ifelse_early_return2(self): + answer = np.zeros([2, 2]) + 3 + static_func = paddle.jit.to_static(dyfunc_with_if_else_early_return2) + out = static_func() + np.testing.assert_allclose(answer, out[0].numpy(), rtol=1e-05) + + +class TestRemoveCommentInDy2St(unittest.TestCase): + def func_with_comment(self): + # Comment1 + x = paddle.to_tensor([1, 2, 3]) + # Comment2 + # Comment3 + y = paddle.to_tensor([4, 5, 6]) + + def test_remove_comment(self): + code_string = func_to_source_code(self.func_with_comment) + self.assertEqual('#' not in code_string, True) + + +if __name__ == '__main__': + unittest.main() From a6e0ebeed4efbbe4ccde16c19ecdd2f28101058c Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 21:13:13 +0800 Subject: [PATCH 14/20] api.py remove get_func --- .../unittests/dygraph_to_static/test_program_translator.py | 2 -- python/paddle/jit/api.py | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py index a1326cd7b8174e..05481ad93f9e61 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py @@ -28,7 +28,6 @@ import paddle.fluid as fluid import paddle.jit.dy2static as _jst from paddle.jit.api import to_static -from paddle.jit.dy2static.program_translator import ProgramTranslator from paddle.jit.dy2static.utils import func_to_source_code from paddle.utils import gast @@ -241,7 +240,6 @@ class TestEnableDeclarative(unittest.TestCase): def setUp(self): self.x = np.random.randn(30, 10, 32).astype('float32') self.weight = np.random.randn(32, 64).astype('float32') - self.program_translator = ProgramTranslator() def test_raise_error(self): with fluid.dygraph.guard(): diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index c4f2e80a2176dd..d49933ebc3e5a8 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -49,6 +49,7 @@ ProgramTranslator, StaticFunction, unwrap_decorators, + convert_to_static, ) from paddle.jit.translated_layer import ( TranslatedLayer, @@ -171,7 +172,7 @@ def __impl__(*args, **kwargs): "We will just return dygraph output." ) return dygraph_func(*args, **kwargs) - static_func = program_translator.get_func(dygraph_func) + static_func = convert_to_static(dygraph_func) return static_func(*args, **kwargs) return __impl__ From 5154f97301fa1156720fe79649086a1438e89a4b Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Sun, 8 Jan 2023 22:13:13 +0800 Subject: [PATCH 15/20] TestDygraphToStaticCode --- .../test_program_translator.py | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py index 05481ad93f9e61..2ce7834779d810 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py @@ -19,7 +19,6 @@ import astor import numpy as np from ifelse_simple_func import ( - dyfunc_with_if_else, dyfunc_with_if_else_early_return1, dyfunc_with_if_else_early_return2, ) @@ -212,30 +211,6 @@ def forward(self, x): return y -class TestDygraphToStaticCode(unittest.TestCase): - def setUp(self): - # set to print all string diff when assertEqual fails - self.maxDiff = None - - def test_decorator(self): - code = paddle.jit.to_static(dyfunc_with_if_else).code - print(code) - answer = get_source_code(StaticCode1.dyfunc_with_if_else) - self.assertEqual( - answer.replace('\n', '').replace(' ', ''), - code.replace('\n', '').replace(' ', ''), - ) - - def test_program_translator(self): - answer = get_source_code(StaticCode2.dyfunc_with_if_else) - code = paddle.jit.to_static(dyfunc_with_if_else).code - print(code) - self.assertEqual( - answer.replace('\n', '').replace(' ', ''), - code.replace('\n', '').replace(' ', ''), - ) - - class TestEnableDeclarative(unittest.TestCase): def setUp(self): self.x = np.random.randn(30, 10, 32).astype('float32') From d82b146a6fb96f6ef03fff641ac621443b42cc84 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Tue, 10 Jan 2023 17:05:54 +0800 Subject: [PATCH 16/20] fix check_type and import err --- python/paddle/jit/__init__.py | 1 - python/paddle/jit/dy2static/program_translator.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index 5ce2711c3f80a4..448316690c5201 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -28,7 +28,6 @@ 'load', 'to_static', 'ignore_module', - 'ProgramTranslator', 'TranslatedLayer', 'set_code_level', 'set_verbosity', diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 34f5facfbd175a..f8a677ec2fe498 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -1274,7 +1274,7 @@ def func(x): enable_to_static_bool, "enable_to_static_bool", bool, - "paddle.jit.to_static", + "paddle.jit.enable_to_static", ) _program_trans = ProgramTranslator() _program_trans.enable(enable_to_static_bool) From a9c4db1dd5fd05e0c67a6b1a657a87d8261bc24d Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Tue, 10 Jan 2023 17:54:26 +0800 Subject: [PATCH 17/20] roll back PT without getcode --- .../jit/dy2static/program_translator.py | 228 ++++++++++++++++++ 1 file changed, 228 insertions(+) diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index f8a677ec2fe498..3e5b0da07581ef 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -1235,6 +1235,234 @@ def func(x): ) self.enable_to_static = enable_to_static + def get_output(self, dygraph_func, *args, **kwargs): + """ + Returns the output dygraph Tensor for dygraph function. The dygraph + function will be translated into static graph function so the under + beneath numerical result will be calculated by static graph mode. + + Args: + dygraph_func (callable): the dygraph function. + *args (tuple): the input argument of dygraph_func. + **kwargs (dict): the input argument of dygraph_func. + + Returns: + Tensor or tuple of Tensors: the dygraph Tensor containing digital result. + + Examples: + .. code-block:: python + + import paddle + + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + + prog_trans = paddle.jit.ProgramTranslator() + + x = paddle.ones([1, 2]) + x_v = prog_trans.get_output(func, x) + print(x_v) # [[0. 0.]] + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_output" + + if not self.enable_to_static: + # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) + # will show up **only once**. + logging_utils.warn( + "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. " + "We will just return dygraph output. " + "Please call ProgramTranslator.enable(True) if you would like to get static output." + ) + return dygraph_func(*args, **kwargs) + try: + function_spec = FunctionSpec(dygraph_func) + cache_key = CacheKey.from_func_and_args( + function_spec, + args, + kwargs, + getattr(dygraph_func, '__self__', None), + ) + _, partial_program_layer = self._program_cache[cache_key] + + if args and isinstance(args[0], layers.Layer): + # Synchronize self.training attribute. + partial_program_layer.training = args[0].training + args = args[1:] + try: + return partial_program_layer(args) + except BaseException as e: + # NOTE: + # 1. If e is raised in compile time, e should have been attached to ERROR_DATA before; + # 2. If e raised in runtime, e should be attached to ERROR_DATA here. + if not hasattr(e, error.ERROR_DATA): + # runtime error + error.attach_error_data(e, in_runtime=True) + raise + except BaseException as e: + error_data = getattr(e, error.ERROR_DATA, None) + if error_data: + error_data.raise_new_exception() + else: + logging_utils.warn( + "Please file an issue at 'https://github.com/PaddlePaddle/Paddle/issues'" + " if you can't handle this {} yourself.".format(type(e)) + ) + raise e + + def get_func(self, dygraph_func): + """ + Returns a callable function which converts imperative dygraph APIs of + the input dygraph_func into declarative net-building APIs, which means + it doesn't return immediate digital result as get_output does. + Users should handle Program and Executor by themselves. + + Args: + dygraph_func (callable): the dygraph function. + + Returns: + callable: converting imperative dygraph APIs into declarative + net-building APIs. + + Examples: + .. code-block:: python + + import paddle + + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + + prog_trans = paddle.jit.ProgramTranslator() + static_func = prog_trans.get_func(func) + print(callable(static_func)) # True + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_func" + + if not self.enable_to_static: + logging_utils.warn( + "The ProgramTranslator.get_func doesn't work when setting ProgramTranslator.enable to False. We will " + "just return dygraph output. Please call ProgramTranslator.enable(True) if you would like to get static output." + ) + return dygraph_func + + static_func = convert_to_static(dygraph_func) + return static_func + + def get_program(self, dygraph_func, *args, **kwargs): + """ + Returns the translated static program and input/output Tensors from + dygraph function. The users can use the program to run by executor. + + Args: + dygraph_func (callable): the dygraph function. + *args (tuple): the input argument of dygraph_func. + **kwargs (dict): the input argument of dygraph_func. + + Returns: + tuple of (main_program, startup_program, inputs, outputs) whose + types are (Program, Program, list of Tensors, list of Tensors). + main_program: the converted main program. + startup_program: the converted startup program. + inputs: list of input Tensors which need to be fed. + outputs: list of output Tensors which users can fetch. + + Examples: + .. code-block:: python + + import paddle + + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + + prog_trans = paddle.jit.ProgramTranslator() + x = paddle.ones([1, 2]) + main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) + print([i.name for i in inputs]) + # [u'generated_tensor_0'] the feed input Tensor name representing x + print([o.name for o in outputs]) + # [u'_generated_var_4'] the fetch output Tensor name representing x_v + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_program" + + if not self.enable_to_static: + logging_utils.warn( + "The ProgramTranslator.get_program doesn't work when setting ProgramTranslator.enable to False." + "We will just return dygraph output. " + "Please call ProgramTranslator.enable(True) if you would like to get static output." + ) + return dygraph_func(*args, **kwargs) + + function_spec = FunctionSpec(dygraph_func) + cache_key = CacheKey.from_func_and_args( + function_spec, args, kwargs, getattr(dygraph_func, '__self__', None) + ) + concrete_program, partial_program_layer = self._program_cache[cache_key] + + # Note: concrete_program hold all input/output infos include non-Variable + input_vars = [ + var + for var in concrete_program.inputs + if isinstance(var, framework.Variable) + ] + output_vars = [ + var + for var in concrete_program.outputs + if isinstance(var, framework.Variable) + ] + + return ( + concrete_program.main_program, + concrete_program.startup_program, + input_vars, + output_vars, + ) + + def get_program_cache(self): + """ + Returns the ProgramCache instance. This method is used by PaddlePaddle + developers to manage program cache in ProgramTranslator. Normal users + don't have to call this method. + + Returns: + ProgramCache: ProgramCache instance of ProgramTranslator. + + Examples: + .. code-block:: python + + import paddle + + prog_trans = paddle.jit.ProgramTranslator() + prog_cache = prog_trans.get_program_cache() + + """ + return self._program_cache + def enable_to_static(enable_to_static_bool): From c176a07e692c6f9d87c28967ab3b29a3e5b0aeb5 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Tue, 10 Jan 2023 18:54:06 +0800 Subject: [PATCH 18/20] roll back pt with get_code --- .../jit/dy2static/program_translator.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 3e5b0da07581ef..7fd6b0ce7fe004 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -14,6 +14,7 @@ import collections import inspect +import textwrap import threading import weakref @@ -41,6 +42,7 @@ from .utils import ( ALREADY_D2S, ast_to_func, + ast_to_source_code, func_to_source_code, input_specs_compatible, make_hashable, @@ -1443,6 +1445,54 @@ def func(x): output_vars, ) + def get_code(self, dygraph_func): + """ + Returns the translated static function string code from dygraph function. + + Args: + dygraph_func (callable): the dygraph function. + + Returns: + str: the string code of translated static function. + + Examples: + .. code-block:: python + + import paddle + + + def func(x): + if paddle.mean(x) > 0: + x_v = x - 1 + else: + x_v = x + 1 + return x_v + + + prog_trans = paddle.jit.ProgramTranslator() + + code = prog_trans.get_code(func) + print(type(code)) # + + """ + assert callable( + dygraph_func + ), "Input dygraph_func is not a callable in ProgramTranslator.get_code" + # Gets AST from dygraph function + + unwrap_func = unwrap(dygraph_func) + raw_code = inspect.getsource(unwrap_func) + code = textwrap.dedent(raw_code) + root = gast.parse(code) + + # Transform AST + dygraph_to_static = DygraphToStaticAst() + root_wrapper = dygraph_to_static.get_static_ast(root) + + # Get source_code + source_code = ast_to_source_code(root_wrapper.node) + return source_code + def get_program_cache(self): """ Returns the ProgramCache instance. This method is used by PaddlePaddle From 11626929b1d20ae9730bb7df4c736464b883c79c Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Tue, 10 Jan 2023 19:23:56 +0800 Subject: [PATCH 19/20] convert_to_static --- python/paddle/jit/api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index c3e87a9756187b..30ad61e0b551fc 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -50,7 +50,6 @@ ProgramTranslator, StaticFunction, unwrap_decorators, - convert_to_static, ) from paddle.jit.translated_layer import ( TranslatedLayer, @@ -173,7 +172,7 @@ def __impl__(*args, **kwargs): "We will just return dygraph output." ) return dygraph_func(*args, **kwargs) - static_func = convert_to_static(dygraph_func) + static_func = program_translator.get_func(dygraph_func) return static_func(*args, **kwargs) return __impl__ From bbf167c836df1a182d8c734e2dacefea7a37b4f9 Mon Sep 17 00:00:00 2001 From: drryanhuang Date: Tue, 10 Jan 2023 20:04:11 +0800 Subject: [PATCH 20/20] fix import __all__ --- python/paddle/jit/__init__.py | 2 +- python/paddle/jit/api.py | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/python/paddle/jit/__init__.py b/python/paddle/jit/__init__.py index 448316690c5201..f508f72478b00b 100644 --- a/python/paddle/jit/__init__.py +++ b/python/paddle/jit/__init__.py @@ -17,8 +17,8 @@ from .api import load from .api import to_static from .api import not_to_static -from .api import enable_to_static from .api import ignore_module +from .dy2static.program_translator import enable_to_static from .dy2static.logging_utils import set_code_level, set_verbosity from .translated_layer import TranslatedLayer diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index 30ad61e0b551fc..ebef5f28654e6f 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -46,7 +46,6 @@ add_ignore_module, ) from .dy2static.program_translator import ( - enable_to_static, ProgramTranslator, StaticFunction, unwrap_decorators, @@ -76,15 +75,6 @@ from paddle.fluid.framework import dygraph_only, _non_static_mode from paddle.fluid.wrapped_decorator import wrap_decorator -__all__ = [ - "save", - "load", - "to_static", - "not_to_static", - "TranslatedLayer", - "enable_to_static", -] - def create_program_from_desc(program_desc): program = Program()