paddle.jit. to_static ( function=None, input_spec=None, build_strategy=None, backend=None, **kwargs ) [source]

Converts imperative dygraph APIs into declarative function APIs. Decorator @to_static handles the Program and Executor of static graph 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. :param function: callable imperative function. :type function: callable :param input_spec: list/tuple of InputSpec to specific the shape/dtype/name

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/jit/ of paddle.jit.api.to_static, line 10)

Unexpected indentation.

information of each input Tensor.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/jit/ of paddle.jit.api.to_static, line 11)

Block quote ends without a blank line; unexpected unindent.

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

  • backend (str, Optional) – Specifies compilation backend, which can be CINN or None. When backend is CINN, CINN compiler will be used to speed up training and inference.

  • kwargs – Support keys including property, set property to True if the fucntion is python property.


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.]]