未验证 提交 3b0d31ab 编写于 作者: H Huihuang Zheng 提交者: GitHub

Modify ProgramTranslator and TracedLayer Doc for API 2.0 (#28509)

Modify ProgramTranslator and TracedLayer Doc for API 2.0
上级 75196cda
...@@ -789,7 +789,7 @@ class ProgramTranslator(object): ...@@ -789,7 +789,7 @@ class ProgramTranslator(object):
x = paddle.ones([1, 2]) x = paddle.ones([1, 2])
# ProgramTranslator is disabled so the func is run in dygraph # ProgramTranslator is disabled so the func is run in dygraph
print(func(x).numpy()) # [[0. 0.]] print(func(x)) # [[0. 0.]]
""" """
check_type(enable_to_static, "enable_to_static", bool, check_type(enable_to_static, "enable_to_static", bool,
...@@ -828,7 +828,7 @@ class ProgramTranslator(object): ...@@ -828,7 +828,7 @@ class ProgramTranslator(object):
x = paddle.ones([1, 2]) x = paddle.ones([1, 2])
x_v = prog_trans.get_output(func, x) x_v = prog_trans.get_output(func, x)
print(x_v.numpy()) # [[0. 0.]] print(x_v) # [[0. 0.]]
""" """
assert callable( assert callable(
......
...@@ -1051,7 +1051,7 @@ class TracedLayer(object): ...@@ -1051,7 +1051,7 @@ class TracedLayer(object):
model and convert it into a static graph model. model and convert it into a static graph model.
Args: Args:
layer (dygraph.Layer): the layer object to be traced. layer (paddle.nn.Layer): the layer object to be traced.
inputs (list(Tensor)|tuple(Tensor)|Tensor): the input tensors of inputs (list(Tensor)|tuple(Tensor)|Tensor): the input tensors of
the layer object. the layer object.
...@@ -1063,23 +1063,20 @@ class TracedLayer(object): ...@@ -1063,23 +1063,20 @@ class TracedLayer(object):
Examples: Examples:
.. code-block:: python: .. code-block:: python:
import paddle.fluid as fluid import paddle
from paddle.fluid.dygraph import Linear, to_variable, TracedLayer
import numpy as np
class ExampleLayer(fluid.dygraph.Layer): class ExampleLayer(paddle.nn.Layer):
def __init__(self): def __init__(self):
super(ExampleLayer, self).__init__() super(ExampleLayer, self).__init__()
self._fc = Linear(3, 10) self._fc = paddle.nn.Linear(3, 10)
def forward(self, input): def forward(self, input):
return self._fc(input) return self._fc(input)
with fluid.dygraph.guard():
layer = ExampleLayer() layer = ExampleLayer()
in_np = np.random.random([2, 3]).astype('float32') in_var = paddle.uniform(shape=[2, 3], dtype='float32')
in_var = to_variable(in_np) out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
out_dygraph, static_layer = TracedLayer.trace(layer, inputs=[in_var])
# run the static graph model using Executor inside # run the static graph model using Executor inside
out_static_graph = static_layer([in_var]) out_static_graph = static_layer([in_var])
...@@ -1089,6 +1086,7 @@ class TracedLayer(object): ...@@ -1089,6 +1086,7 @@ class TracedLayer(object):
# save the static graph model for inference # save the static graph model for inference
static_layer.save_inference_model(dirname='./saved_infer_model') static_layer.save_inference_model(dirname='./saved_infer_model')
""" """
assert isinstance( assert isinstance(
layer, Layer layer, Layer
...@@ -1114,33 +1112,30 @@ class TracedLayer(object): ...@@ -1114,33 +1112,30 @@ class TracedLayer(object):
Examples: Examples:
.. code-block:: python: .. code-block:: python:
import paddle.fluid as fluid import paddle
from paddle.fluid.dygraph import Linear, to_variable, TracedLayer
import numpy as np
class ExampleLayer(fluid.dygraph.Layer): class ExampleLayer(paddle.nn.Layer):
def __init__(self): def __init__(self):
super(ExampleLayer, self).__init__() super(ExampleLayer, self).__init__()
self._fc = Linear(3, 10) self._fc = paddle.nn.Linear(3, 10)
def forward(self, input): def forward(self, input):
return self._fc(input) return self._fc(input)
with fluid.dygraph.guard():
layer = ExampleLayer() layer = ExampleLayer()
in_np = np.random.random([2, 3]).astype('float32') in_var = paddle.uniform(shape=[2, 3], dtype='float32')
in_var = to_variable(in_np)
out_dygraph, static_layer = TracedLayer.trace(layer, inputs=[in_var]) out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
build_strategy = fluid.BuildStrategy() build_strategy = paddle.static.BuildStrategy()
build_strategy.enable_inplace = True build_strategy.enable_inplace = True
exec_strategy = fluid.ExecutionStrategy() exec_strategy = paddle.static.ExecutionStrategy()
exec_strategy.num_threads = 2 exec_strategy.num_threads = 2
static_layer.set_strategy(build_strategy=build_strategy, exec_strategy=exec_strategy) static_layer.set_strategy(build_strategy=build_strategy, exec_strategy=exec_strategy)
out_static_graph = static_layer([in_var]) out_static_graph = static_layer([in_var])
""" """
assert self._compiled_program is None, "Cannot set strategy after run" assert self._compiled_program is None, "Cannot set strategy after run"
assert isinstance( assert isinstance(
...@@ -1212,30 +1207,29 @@ class TracedLayer(object): ...@@ -1212,30 +1207,29 @@ class TracedLayer(object):
Examples: Examples:
.. code-block:: python: .. code-block:: python:
import paddle.fluid as fluid
from paddle.fluid.dygraph import Linear, to_variable, TracedLayer
import numpy as np import numpy as np
import paddle
class ExampleLayer(fluid.dygraph.Layer): class ExampleLayer(paddle.nn.Layer):
def __init__(self): def __init__(self):
super(ExampleLayer, self).__init__() super(ExampleLayer, self).__init__()
self._fc = Linear(3, 10) self._fc = paddle.nn.Linear(3, 10)
def forward(self, input): def forward(self, input):
return self._fc(input) return self._fc(input)
save_dirname = './saved_infer_model' save_dirname = './saved_infer_model'
in_np = np.random.random([2, 3]).astype('float32') in_np = np.random.random([2, 3]).astype('float32')
in_var = paddle.to_tensor(in_np)
with fluid.dygraph.guard():
layer = ExampleLayer() layer = ExampleLayer()
in_var = to_variable(in_np)
out_dygraph, static_layer = TracedLayer.trace(layer, inputs=[in_var]) out_dygraph, static_layer = paddle.jit.TracedLayer.trace(layer, inputs=[in_var])
static_layer.save_inference_model(save_dirname, feed=[0], fetch=[0]) static_layer.save_inference_model(save_dirname, feed=[0], fetch=[0])
place = fluid.CPUPlace() paddle.enable_static()
exe = fluid.Executor(place) place = paddle.CPUPlace()
program, feed_vars, fetch_vars = fluid.io.load_inference_model(save_dirname, exe = paddle.static.Executor(place)
program, feed_vars, fetch_vars = paddle.static.load_inference_model(save_dirname,
exe) exe)
fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars) fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册