to_static

paddle.jit. to_static ( function: _LayerT, input_spec: NestedStructure[InputSpec] | None = None, build_strategy: paddle.base.libpaddle.CompiledProgram.BuildStrategy | None = None, backend: Optional[Literal['CINN']] = 'CINN', **kwargs: Unpack ) _LayerT [source]
paddle.jit. to_static ( function: Callable[[_InputT], _RetT], input_spec: NestedStructure[InputSpec] | None = None, build_strategy: paddle.base.libpaddle.CompiledProgram.BuildStrategy | None = None, backend: Optional[Literal['CINN']] = 'CINN', **kwargs: Unpack ) StaticFunction[_InputT, _RetT]
paddle.jit. to_static ( function: None = None, input_spec: NestedStructure[InputSpec] | None = None, build_strategy: paddle.base.libpaddle.CompiledProgram.BuildStrategy | None = None, backend: Optional[Literal['CINN']] = 'CINN', **kwargs: Unpack ) _ToStaticDecorator

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

Parameters
  • function (callable) – Callable dynamic graph function. If it used as a decorator, the decorated function will be parsed as this parameter.

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

  • 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. default value is "CINN".

  • kwargs

    Support keys including property and full_graph.

    • property (bool): If True, the function will be treated as a property function. The default is False.

    • full_graph (bool): If True, the function will be converted into a full static graph. The default is False.

Returns

containing the numerical result.

Return type

Tensor(s)

Examples

>>> 
>>> import paddle
>>> from paddle.jit import to_static

>>> @to_static
>>> def func(x):
...     if paddle.mean(x) < 0:
...         x_v = x - 1
...     else:
...         x_v = x + 1
...     return x_v
...
>>> x = paddle.ones([1, 2], dtype='float32')
>>> x_v = func(x)
>>> print(x_v)
Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[2., 2.]])