|
|
|
@ -29,7 +29,7 @@ from paddle.fluid.dygraph.dygraph_to_static.ast_transformer import DygraphToStat
|
|
|
|
|
from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code
|
|
|
|
|
from paddle.fluid.framework import in_dygraph_mode
|
|
|
|
|
|
|
|
|
|
__all__ = ['ProgramTranslator']
|
|
|
|
|
__all__ = ['ProgramTranslator', 'convert_function_with_cache']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionCache(object):
|
|
|
|
@ -66,6 +66,19 @@ class FunctionCache(object):
|
|
|
|
|
self._get_dedent_code_string(func), None) is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_CACHE_LOCK = threading.Lock()
|
|
|
|
|
_FUNCTION_CACHE = FunctionCache()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_function_with_cache(dygraph_func):
|
|
|
|
|
"""
|
|
|
|
|
Transform function of dygraph into static function using the cache mechanism.
|
|
|
|
|
"""
|
|
|
|
|
with _CACHE_LOCK:
|
|
|
|
|
static_func = _FUNCTION_CACHE.get_or_cache_func(dygraph_func)
|
|
|
|
|
return static_func
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def synchronized(func):
|
|
|
|
|
func.__lock__ = threading.Lock()
|
|
|
|
|
|
|
|
|
@ -273,7 +286,7 @@ class ProgramTranslator(object):
|
|
|
|
|
"The decorator 'dygraph_to_static_graph' doesn't work in dygraph mode."
|
|
|
|
|
" Please use it in static mode.")
|
|
|
|
|
return dygraph_func
|
|
|
|
|
static_func, ast_transformer = convert_to_static(dygraph_func)
|
|
|
|
|
static_func = convert_function_with_cache(dygraph_func)
|
|
|
|
|
return static_func
|
|
|
|
|
|
|
|
|
|
def get_code(self, dygraph_func):
|
|
|
|
|