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):

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.enable, line 18)

Unexpected indentation.

x = fluid.dygraph.to_variable(x) if fluid.layers.mean(x) > 0:

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.enable, line 20)

Unexpected indentation.

x_v = x - 1

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.enable, line 21)

Block quote ends without a blank line; unexpected unindent.

else:

x_v = x + 1

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.enable, line 23)

Definition list ends without a blank line; unexpected unindent.

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():

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.enable, line 31)

Unexpected indentation.

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,) –

    System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.get_output, line 7); backlink

    Inline strong start-string without end-string.

    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,) –

    System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.get_program, line 6); backlink

    Inline strong start-string without end-string.

    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:

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.get_code, line 19)

Unexpected indentation.

x_v = x - 1

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.get_code, line 20)

Block quote ends without a blank line; unexpected unindent.

else:

x_v = x + 1

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.get_code, line 22)

Definition list ends without a blank line; unexpected unindent.

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.

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.save_inference_model, line 1); backlink

Inline interpreted text or phrase reference start-string without end-string.

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

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/dygraph_to_static/program_translator.py:docstring of paddle.fluid.dygraph.ProgramTranslator.save_inference_model, line 23)

Error in “code-block” directive: maximum 1 argument(s) allowed, 21 supplied.

.. code-block:: python
    import numpy as np
    import paddle.fluid as fluid
    from paddle.fluid.dygraph import Linear
    from paddle.fluid.dygraph import declarative
    from paddle.fluid.dygraph import ProgramTranslator

    class SimpleNet(fluid.dygraph.Layer):
        def __init__(self, in_size, out_size):
            super(SimpleNet, self).__init__()
            self._linear = Linear(in_size, out_size)

        @declarative
        def forward(self, x):
            y = self._linear(x)
            z = self._linear(y)
            loss = fluid.layers.mean(z)
            return z, loss

    with fluid.dygraph.guard(fluid.CPUPlace()):
        net = SimpleNet(8, 8)
        adam = fluid.optimizer.AdamOptimizer(learning_rate=0.1, parameter_list=net.parameters())
        x = fluid.dygraph.to_variable(np.random.random((4, 8)).astype('float32'))
        for i in range(10):
            loss, out = net(x)
            loss.backward()
            adam.minimize(loss)
            net.clear_gradients()
    # Save inference model.
    # Note that fetch=[0] means we set 'y' as the inference output.
    prog_trans = ProgramTranslator()
    prog_trans.save_inference_model("./dy2stat_infer_model", fetch=[0])

    # In this example, the inference model will be pruned based on input (x) and
    # output (y). The pruned inference program is going to be saved in the folder
    # "./dy2stat_infer_model" and parameters are going to be saved in separate
    # files in the folder.
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()