Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
475 changes: 475 additions & 0 deletions docs/guides/04_dygraph_to_static/basic_usage_cn.md

Large diffs are not rendered by default.

140 changes: 0 additions & 140 deletions docs/guides/04_dygraph_to_static/basic_usage_cn.rst

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# 基本原理
# 基本用法


## 一、 @to_static概览

动静转换(@to_static)通过解析 Python代码(抽象语法树,下简称:AST) 实现一行代码即可转为静态图功能,即只需在待转化的函数前添加一个装饰器 ``@paddle.jit.to_static`` 。

如下是一个使用 @to_static 装饰器的 ``Model`` 示例:

```python
Expand All @@ -14,7 +16,7 @@ class SimpleNet(paddle.nn.Layer):
super(SimpleNet, self).__init__()
self.linear = paddle.nn.Linear(10, 3)

# 方式一:装饰 forward 函数
# 方式一:装饰 forward 函数(支持训练)
@to_static
def forward(self, x, y):
out = self.linear(x)
Expand All @@ -26,6 +28,12 @@ net = SimpleNet()
net = paddle.jit.to_static(net) # 动静转换
```

动转静 @to_static 除了支持预测模型导出,还兼容转为静态图子图训练。仅需要在 ``forward`` 函数上添加此装饰器即可,不需要修改任何其他的代码。

基本执行流程如下:

<img src="https://raw.githubusercontent.com/PaddlePaddle/docs/develop/docs/guides/04_dygraph_to_static/images/to_static_train.png" style="zoom:50%" />


### 1.1 动态图 layer 生成 Program

Expand Down Expand Up @@ -243,7 +251,7 @@ def add_two(x, y):
+ **只有**控制流的判断条件**依赖了 ``Tensor`` **(如 ``shape`` 或 ``value`` ),才会转写为对应 Op


![image](./images/convert_cond.png)
<img src="https://raw.githubusercontent.com/PaddlePaddle/docs/develop/docs/guides/04_dygraph_to_static/images/convert_cond.png" style="zoom:50%" />



Expand Down Expand Up @@ -324,7 +332,7 @@ def depend_tensor_if(x):
```


``conver_ifelse`` 是框架底层的函数,在逐行执行用户代码生成 ``Program`` 时,执行到此处时,会根据**判断条件**的类型( ``bool`` 还是 ``Bool Tensor`` ),自适应决定是否转为 ``cond_op`` 。
``convert_ifelse`` 是框架底层的函数,在逐行执行用户代码生成 ``Program`` 时,执行到此处时,会根据**判断条件**的类型( ``bool`` 还是 ``Bool Tensor`` ),自适应决定是否转为 ``cond_op`` 。

```python
def convert_ifelse(pred, true_fn, false_fn, true_args, false_args, return_vars):
Expand Down Expand Up @@ -464,11 +472,3 @@ class SimpleNet(paddle.nn.Layer):

+ 若某个非 ``Tensor`` 数据需要当做 ``Persistable`` 的变量序列化到磁盘,则最好在 ``__init__`` 中调用 ``self.XX= paddle.to_tensor(xx)`` 接口转为 ``buffer`` 变量

## 六、Program 执行与训练

动转静 @to_static 除了支持预测模型导出,还兼容转为静态图子图训练。仅需要在 ``forward`` 函数上添加此装饰器即可。

基本执行流程如下:


![image](./images/to_static_train.png)
134 changes: 0 additions & 134 deletions docs/guides/04_dygraph_to_static/basic_usage_en.rst

This file was deleted.

Loading