to_static¶
- paddle.jit. to_static ( function=None, input_spec=None, build_strategy=None, backend=None, **kwargs ) [source]
-
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.
kwargs – Support keys including property, set property to True if the function is python property.
- 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.]])