ProgramTranslator¶
-
class
paddle.fluid.dygraph.
ProgramTranslator
[source] Class to translate dygraph function into static graph function. The object of this class is a singleton.
- Parameters
None. –
- Returns
the singleton object.
- Return type
ProgramTranslator
Examples
import paddle.fluid as fluid
# Two methods get same object because ProgramTranslator is a singleton fluid.dygraph.ProgramTranslator() fluid.dygraph.ProgramTranslator.get_instance()
-
enable
(enable_declarative) Enable or disable the converting from imperative to declarative by ProgramTranslator globally.
- Parameters
enable_declarative (bool) – True or False to enable or disable declarative.
- Returns
None.
Examples
import paddle.fluid as fluid import numpy as np
@fluid.dygraph.jit.declarative def func(x):
x = fluid.dygraph.to_variable(x) if fluid.layers.mean(x) > 0:
x_v = x - 1
- else:
x_v = x + 1
return x_v
prog_trans = fluid.dygraph.ProgramTranslator() prog_trans.enable(False)
x = np.ones([1, 2]) # The declarative is disabled so the func is run in dygraph with fluid.dygraph.guard():
print(func(x).numpy()) # [[2. 2.]]
-
get_output
(dygraph_func, *args, **kwargs) Returns the output dygraph VarBase for dygraph function. The dygraph function will be translated into static graph function so the under beneath numerical result will be calculated by declarative mode.
- Parameters
dygraph_func (callable) – the dygraph function.
**kwargs (*args,) –
the input argument of dygraph_func.
- Returns
- the dygraph VarBase containing digital
result.
- Return type
VarBase or tuple of VarBase
Examples
import paddle.fluid as fluid import numpy as np def func(x): x = fluid.dygraph.to_variable(x) if fluid.layers.mean(x) > 0: x_v = x - 1 else: x_v = x + 1 return x_v prog_trans = fluid.dygraph.ProgramTranslator() with fluid.dygraph.guard(): x = np.ones([1, 2]) x_v = prog_trans.get_output(func, x) print(x_v.numpy()) # [[0. 0.]]
-
get_func
(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.
- Parameters
dygraph_func (callable) – the dygraph function.
- Returns
converting imperative dygraph APIs into declarative net-building APIs.
- Return type
callable
Examples
import paddle.fluid as fluid import numpy as np def func(x): x = fluid.dygraph.to_variable(x) if fluid.layers.mean(x) > 0: x_v = x - 1 else: x_v = x + 1 return x_v prog_trans = fluid.dygraph.ProgramTranslator() static_func = prog_trans.get_func(func) print(callable(static_func)) # True
-
get_program
(dygraph_func, *args, **kwargs) Returns the translated static program and input/output variables from dygraph function. The users can use the program to run by executor.
- Parameters
dygraph_func (callable) – the dygraph function.
**kwargs (*args,) –
the input argument of dygraph_func.
- Returns
tuple of (main_program, startup_program, inputs, outputs) whose types are (Program, Program, list of Variable, list of Variable). main_program: the converted main program. startup_program: the converted startup program. inputs: list of input Variables which need to be fed. outputs: list of output Variables which users can fetch.
Examples
import paddle.fluid as fluid import numpy as np def func(x): x = fluid.dygraph.to_variable(x) if fluid.layers.mean(x) > 0: x_v = x - 1 else: x_v = x + 1 return x_v prog_trans = fluid.dygraph.ProgramTranslator() x = np.ones([1, 2]) main_prog, start_prog, inputs, outputs = prog_trans.get_program(func, x) print([i.name for i in inputs]) # ['feed_0'] the feed input variable name representing x print([o.name for o in outputs]) # ['_generated_var_4'] the fetch output variable name representing x_v
-
get_code
(dygraph_func) Returns the translated static function string code from dygraph function.
- Parameters
dygraph_func (callable) – the dygraph function.
- Returns
the string code of translated static function.
- Return type
str
Examples
import paddle.fluid as fluid import numpy as np
- def func(x):
x = fluid.dygraph.to_variable(x) if fluid.layers.mean(x) > 0:
x_v = x - 1
- else:
x_v = x + 1
return x_v
prog_trans = fluid.dygraph.ProgramTranslator()
code = prog_trans.get_code(func) print(type(code)) # <class ‘str’>
-
save_inference_model
(dirname, feed=None, fetch=None) Saves current model as the inference model. It will prune the main_program to build a new program especially for inference, and then save it and all related parameters to given dirname . The saved inference model can be loaded by :ref:`api_fluid_io_load_inference_model or `C++ inference APIs.
- Parameters
dirname (str) – the directory to save the inference model.
feed (list[int], optional) – the input variable indices of the saved inference model. If None, all input variables of the ProgramTranslator would be the inputs of the saved inference model. Default None.
fetch (list[int], optional) – the output variable indices of the saved inference model. If None, all output variables of the TracedLayer object would be the outputs of the saved inference model. Default None.
- Returns
None
Examples
-
get_program_cache
() 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 instance of ProgramTranslator.
- Return type
ProgramCache
Examples
import paddle.fluid as fluid prog_trans = fluid.dygraph.ProgramTranslator() prog_cache = prog_trans.get_program_cache()