ProgramTranslator

class paddle.jit. ProgramTranslator ( **kwargs ) [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

# Two methods get same object because ProgramTranslator is a singleton
paddle.jit.ProgramTranslator()
paddle.jit.ProgramTranslator.get_instance()
enable ( enable_to_static )

enable

Enable or disable the converting from imperative to static graph by ProgramTranslator globally.

Parameters

enable_to_static (bool) – True or False to enable or disable converting to static.

Returns

None.

Examples

import paddle


@paddle.jit.to_static
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()
prog_trans.enable(False)

x = paddle.ones([1, 2])
# ProgramTranslator is disabled so the func is run in dygraph
print(func(x))  # [[0. 0.]]
get_output ( dygraph_func, *args, **kwargs )

get_output

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.

Parameters
  • dygraph_func (callable) – the dygraph function.

  • *args (tuple) – the input argument of dygraph_func.

  • **kwargs (dict) – the input argument of dygraph_func.

Returns

the dygraph Tensor containing digital result.

Return type

Tensor or tuple of Tensors

Examples

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.]]
get_func ( dygraph_func )

get_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


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
get_program ( dygraph_func, *args, **kwargs )

get_program

Returns the translated static program and input/output Tensors from dygraph function. The users can use the program to run by executor.

Parameters
  • 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

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
get_code ( dygraph_func )

get_code

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


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)) # <class 'str'>
get_program_cache ( )

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

prog_trans = paddle.jit.ProgramTranslator()
prog_cache = prog_trans.get_program_cache()