paddle.jit. to_static ( function=None, input_spec=None, build_strategy=None, property=False ) [source]

Converts imperative dygraph APIs into declarative function APIs. Decorator @declarative handles the Program and Executor of static mode and returns the result as dygraph Tensor(s). Users could use the returned dygraph Tensor(s) to do imperative training, inference, or other operations. If the decorated function calls other imperative function, the called one will be converted into declarative function as well.

  • function (callable) – callable imperative function.

  • input_spec (list[InputSpec]|tuple[InputSpec]) – list/tuple of InputSpec to specific the shape/dtype/name information of each input Tensor.

  • build_strategy (BuildStrategy|None) – This argument is used to compile the converted program with the specified options, such as operators’ fusion in the computational graph and memory optimization during the execution of the computational graph. For more information about build_strategy, please refer to paddle.static.BuildStrategy. The default is None.

  • property (bool, Optional) – whether the fucntion is python property. The default is False.


containing the numerical result.

Return type



import paddle
from paddle.jit import to_static

def func(x):
    if paddle.mean(x) < 0:
        x_v = x - 1
        x_v = x + 1
    return x_v

x = paddle.ones([1, 2], dtype='float32')
x_v = func(x)
print(x_v) # [[2. 2.]]