-
Notifications
You must be signed in to change notification settings - Fork 874
动转静概述更新 #4099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
动转静概述更新 #4099
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,11 +2,46 @@ | |||||
| 动态图转静态图 | ||||||
| ############### | ||||||
|
|
||||||
| 动态图在接口易用性,交互式调试等方面具有诸多优势,但在工业界的许多部署场景中(如大型推荐系统、移动端)Python执行开销较大,与C++有一定的差距,静态图部署更具优势。 | ||||||
| ========================= | ||||||
| 什么是动态图和静态图? | ||||||
| ========================= | ||||||
|
|
||||||
| PaddlePaddle 在2.0版本之后,正式支持动态图转静态图(@to_static)的功能,对动态图代码进行智能化分析,自动转换为静态图网络结构,兼顾了动态图易用性和静态图部署性能两方面的优势。 | ||||||
| 在深度学习模型构建上,飞桨框架支持动态图编程和静态图编程两种方式,其代码编写和执行方式均存在差异。 | ||||||
|
|
||||||
| 如下将详细地介绍动静转换的各个模块内容: | ||||||
| * **动态图编程:** 采用 Python 的编程风格,解析式地执行每一行网络代码,并同时返回计算结果。在 `模型开发 <../02_paddle2.0_develop/index_cn.html>`_ 章节中,介绍的都是动态图编程方式。 | ||||||
|
|
||||||
| * **静态图编程:** 采用先编译后执行的方式。需先在代码中预定义完整的神经网络结构,飞桨框架会将神经网络描述为 `Program` 的数据结构,并对 `Program` 进行编译优化,再调用执行器获得计算结果。 | ||||||
|
|
||||||
| 动态图编程体验更佳、更易调试,但是因为采用 Python 实时执行的方式,开销较大,在性能方面与 C++ 有一定差距;静态图调试难度大,但是将前端 Python 编写的神经网络预定义为 Program描述,转到 C++ 端重新解析执行,脱离了 Python 依赖,往往执行性能更佳,并且预先拥有完整网络结构也更利于全局优化。 | ||||||
|
|
||||||
| ========================= | ||||||
| 什么场景下需要动态图转静态图? | ||||||
| ========================= | ||||||
|
|
||||||
| 飞桨框架在设计时,考虑同时兼顾动态图的高易用性和静态图的高性能优势,采用『动静统一』的方案: | ||||||
|
|
||||||
| * **在模型开发时,推荐采用动态图编程。** 可获得更好的编程体验、更易用的接口、更友好的调试交互机制。 | ||||||
|
|
||||||
| * **在模型训练或者推理部署时,只需添加一行装饰器 @to_static,即可将动态图代码转写为静态图代码,并在底层自动使用静态图执行器运行。** 可获得更好的模型运行性能。 | ||||||
|
|
||||||
| 方案如下图所示: | ||||||
|
|
||||||
| .. figure:: images/dygraph_to_static.png | ||||||
| :width: 800px | ||||||
| :align: center | ||||||
|
|
||||||
|
|
||||||
| .. centered:: 图1 飞桨框架动静统一方案示意图 | ||||||
|
|
||||||
|
|
||||||
| .. note:: | ||||||
| 飞桨框架 2.0 及以上版本默认的编程模式是动态图模式,包括使用高层 API 编程和基础的 API 编程。如果想切换到静态图模式编程,可以在程序的开始执行 `enable_static()` 函数。如果程序已经使用动态图的模式编写了,想转成静态图模式训练或者保存模型用于部署,可以使用装饰器 @to_static。 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 添加note时,下面第一行不用空格,顶头写就好,否则看起来展示效果会有问题
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不加空格,注解的格式出不来。 |
||||||
|
|
||||||
| 想了解动态图和静态图的详细对比介绍,可参见 | ||||||
| `动态图和静态图的差异 <https://www.paddlepaddle.org.cn/tutorials/projectdetail/2134396#anchor-8>`_。 | ||||||
|
|
||||||
|
|
||||||
| **以下将详细地介绍动静转换的各个模块内容:** | ||||||
|
|
||||||
| - `基础接口用法 <basic_usage_cn.html>`_ : 介绍了动静转换 @to_static 的基本用法 | ||||||
|
|
||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个居中语法似乎没有生效