Skip to content

Commit

Permalink
Add new paddle.save/load cn doc (PaddlePaddle#2669)
Browse files Browse the repository at this point in the history
* add paddle new save load doc

* move static save apis & polish multiple details

* add api mapping

* move apis place

* fix example code error

* polish model save load guide

* polish example code

* fix details example error

* polish guide doc

* change title format

* remove history reason

* remove other history reason

* add enable_static for old save/load

* add enable_static for old api
  • Loading branch information
chenwhql authored Sep 24, 2020
1 parent c60f039 commit 4f6c833
Show file tree
Hide file tree
Showing 25 changed files with 390 additions and 622 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ trace是指在模型运行时记录下其运行过哪些算子。TracedLayer就
place = paddle.CPUPlace()
exe = paddle.Executor(place)
program, feed_vars, fetch_vars = paddle.io.load_inference_model(save_dirname, exe)
program, feed_vars, fetch_vars = paddle.static.load_inference_model(save_dirname, exe)
fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Load model and run it in static graph mode:
place = paddle.CPUPlace()
exe = paddle.Executor(place)
program, feed_vars, fetch_vars = paddle.io.load_inference_model(save_dirname, exe)
program, feed_vars, fetch_vars = paddle.static.load_inference_model(save_dirname, exe)
fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
However, as tracing only records operators once, if user's code contains Tensor-dependent (including Tensor value or Tensor shape) control flow, that is the Tensor can cause different operators being executed, then TracedLayer cannot handle this case. For instance:
Expand Down
4 changes: 2 additions & 2 deletions doc/fluid/api_cn/fluid_cn/save_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ save
.. py:function:: paddle.fluid.save(program, model_path)
:api_attr: 声明式编程模式(静态图)
:alias_main: paddle.save
:alias: paddle.save,paddle.tensor.save,paddle.tensor.io.save
:alias_main: paddle.static.save
:alias: paddle.static.save
:old_api: paddle.fluid.save


Expand Down
4 changes: 2 additions & 2 deletions doc/fluid/api_cn/io_cn/load_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ load
.. py:function:: paddle.fluid.io.load(program, model_path, executor=None, var_list=None)
:api_attr: 声明式编程模式(静态图)
:alias_main: paddle.load
:alias: paddle.load,paddle.tensor.load,paddle.tensor.io.load
:alias_main: paddle.static.load
:alias: paddle.static.load
:old_api: paddle.fluid.io.load


Expand Down
2 changes: 1 addition & 1 deletion doc/fluid/beginners_guide/dygraph/DyGraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ static_layer.save_inference_model(save_dirname, feed=[0], fetch=[0])
# 声明式编程中需要使用执行器执行之前已经定义好的网络
place = paddle.CPUPlace()
exe = paddle.Executor(place)
program, feed_vars, fetch_vars = paddle.io.load_inference_model(save_dirname, exe)
program, feed_vars, fetch_vars = paddle.static.load_inference_model(save_dirname, exe)
# 声明式编程中需要调用执行器的run方法执行计算过程
fetch, = exe.run(program, feed={feed_vars[0]: in_np}, fetch_list=fetch_vars)
```
Expand Down
8 changes: 4 additions & 4 deletions doc/paddle/api/alias_api_mapping
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ paddle.nn.functional.common.alpha_dropout paddle.nn.functional.alpha_dropout
paddle.nn.functional.activation.log_sigmoid paddle.nn.functional.log_sigmoid
paddle.fluid.executor.Executor paddle.static.Executor
paddle.nn.functional.pooling.avg_pool2d paddle.nn.functional.avg_pool2d
paddle.fluid.dygraph.checkpoint.load_dygraph paddle.load,paddle.framework.load
paddle.framework.io.load paddle.load,paddle.framework.load
paddle.fluid.dygraph.container.Sequential paddle.nn.Sequential
paddle.fluid.dygraph.BilinearTensorProduct paddle.nn.BilinearTensorProduct,paddle.nn.layer.BilinearTensorProduct,paddle.nn.layer.common.BilinearTensorProduct
paddle.fluid.layers.box_coder paddle.nn.functional.box_coder,paddle.nn.functional.vision.box_coder
Expand Down Expand Up @@ -251,7 +251,7 @@ paddle.nn.functional.activation.prelu paddle.nn.functional.prelu
paddle.tensor.linalg.matmul paddle.matmul,paddle.tensor.matmul
paddle.fluid.layers.generate_proposals paddle.nn.functional.generate_proposals,paddle.nn.functional.vision.generate_proposals
paddle.nn.layer.loss.SmoothL1Loss paddle.nn.SmoothL1Loss,paddle.nn.layer.SmoothL1Loss
paddle.fluid.dygraph.checkpoint.save_dygraph paddle.save,paddle.framework.save
paddle.framework.io.save paddle.save,paddle.framework.save
paddle.fluid.core paddle.framework.core
paddle.nn.functional.vision.grid_sample paddle.nn.functional.grid_sample
paddle.tensor.random.rand paddle.rand,paddle.tensor.rand
Expand Down Expand Up @@ -282,7 +282,7 @@ paddle.framework.get_default_dtype paddle.get_default_dtype
paddle.fluid.layers.atan paddle.atan,paddle.tensor.atan,paddle.tensor.math.atan
paddle.fluid.layers.pad2d paddle.nn.functional.pad2d,paddle.nn.functional.common.pad2d
paddle.nn.layer.pooling.MaxPool3d paddle.nn.MaxPool3d,paddle.nn.layer.MaxPool3d
paddle.fluid.io.load paddle.static.load,paddle.tensor.load,paddle.tensor.io.load
paddle.fluid.io.load paddle.static.load
paddle.fluid.dygraph.learning_rate_scheduler.ExponentialDecay paddle.ExponentialDecay,paddle.framework.ExponentialDecay
paddle.fluid.layers.conv3d paddle.static.nn.conv3d
paddle.nn.layer.activation.Softmax paddle.nn.Softmax
Expand Down Expand Up @@ -447,7 +447,7 @@ paddle.tensor.stat.numel paddle.numel,paddle.tensor.numel
paddle.nn.functional.activation.relu6 paddle.nn.functional.relu6
paddle.tensor.math.cumsum paddle.cumsum,paddle.tensor.cumsum
paddle.fluid.layers.resize_trilinear paddle.nn.functional.resize_trilinear,paddle.nn.functional.vision.resize_trilinear
paddle.fluid.save paddle.static.save,paddle.tensor.save,paddle.tensor.io.save
paddle.fluid.io.save paddle.static.save
paddle.fluid.layers.scale paddle.scale,paddle.tensor.scale,paddle.tensor.math.scale
paddle.fluid.framework.default_startup_program paddle.static.default_startup_program
paddle.fluid.layers.noam_decay paddle.nn.functional.noam_decay,paddle.nn.functional.learning_rate.noam_decay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ load
----


.. py:function:: paddle.load(model_path, config=None)
.. py:function:: paddle.fluid.dygraph.load_dygraph(model_path, config=None)
该接口用于从磁盘中加载Layer和Optimizer的 ``state_dict`` ,该接口会同时加载 ``model_path + ".pdparams"`` 和 ``model_path + ".pdopt"`` 中的内容。

.. note::
由于一些历史原因,如果从 ``paddle.io.save_inference_model`` 的存储结果中载入 ``state_dict`` ,动态图模式下参数的结构性变量名将无法被恢复。并且在将载入的 ``state_dict`` 配置到当前Layer中时,需要配置 ``Layer.set_state_dict`` 的参数 ``use_structured_name=False`` 。
如果从 ``paddle.static.save_inference_model`` 的存储结果中载入 ``state_dict`` ,动态图模式下参数的结构性变量名将无法被恢复。并且在将载入的 ``state_dict`` 配置到当前Layer中时,需要配置 ``Layer.set_state_dict`` 的参数 ``use_structured_name=False`` 。

参数:
- **model_path** (str) – 保存state_dict的文件前缀。该路径不应该包括后缀 ``.pdparams`` 或 ``.pdopt``。
- **config** (SaveLoadConfig, 可选) - 用于指定额外配置选项的 :ref:`cn_api_fluid_dygraph_jit_SaveLoadConfig` 对象,这些选项主要是用于兼容 ``paddle.io.save_inference_model`` 存储模型的格式。默认为 ``None``。
- **config** (SaveLoadConfig, 可选) - 用于指定额外配置选项的 :ref:`cn_api_fluid_dygraph_jit_SaveLoadConfig` 对象,这些选项主要是用于兼容 ``paddle.static.save_inference_model`` 存储模型的格式。默认为 ``None``。


返回: 两个 ``dict`` ,即从文件中恢复的模型参数 ``dict`` 和优化器参数 ``dict``,如果只找到其中一个的存储文件,另一个返回None
Expand All @@ -29,23 +29,24 @@ load
.. code-block:: python
import paddle
import paddle.fluid as fluid
paddle.disable_static()
emb = paddle.nn.Embedding([10, 10])
emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict()
paddle.save(state_dict, "paddle_dy")
fluid.save_dygraph(state_dict, "paddle_dy")
scheduler = paddle.optimizer.lr_scheduler.NoamLR(
d_model=0.01, warmup_steps=100, verbose=True)
adam = paddle.optimizer.Adam(
learning_rate=scheduler,
parameters=emb.parameters())
state_dict = adam.state_dict()
paddle.save(state_dict, "paddle_dy")
fluid.save_dygraph(state_dict, "paddle_dy")
para_state_dict, opti_state_dict = paddle.load("paddle_dy")
para_state_dict, opti_state_dict = fluid.load_dygraph("paddle_dy")
27 changes: 15 additions & 12 deletions doc/paddle/api/paddle/fluid/dygraph/jit/SaveLoadConfig_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SaveLoadConfig

.. py:class:: paddle.SaveLoadConfig()
用于配置接口 :ref:`cn_api_fluid_dygraph_jit_save`:ref:`cn_api_fluid_dygraph_jit_load` 存储载入 :ref:`cn_api_fluid_dygraph_TranslatedLayer` 时的附加选项
用于配置接口 ``paddle.jit.save/load````paddle.load`` 存储载入时的附加选项

**示例代码:**

Expand Down Expand Up @@ -74,10 +74,10 @@ SaveLoadConfig

.. py:attribute:: output_spec
选择保存模型( :ref:`cn_api_fluid_dygraph_TranslatedLayer` )的输出变量,通过指定的这些变量能够使模型仅计算特定的结果。
默认情况下,原始 :ref:`cn_api_fluid_dygraph_Layer` 的forward方法的所有返回变量都将配置为存储后模型 :ref:`cn_api_fluid_dygraph_TranslatedLayer` 的输出变量。
选择保存模型( ``paddle.jit.TranslatedLayer`` )的输出变量,通过指定的这些变量能够使模型仅计算特定的结果。
默认情况下,原始 ``paddle.nn.Layer`` 的forward方法的所有返回变量都将配置为存储后模型 ``paddle.jit.TranslatedLayer`` 的输出变量。

``output_spec`` 属性类型需要是 ``list[Variable]``。如果输入的 ``output_spec`` 列表不是原始 :ref:`cn_api_fluid_dygraph_Layer` 的forward方法的所有返回变量,
``output_spec`` 属性类型需要是 ``list[Variable]``。如果输入的 ``output_spec`` 列表不是原始 ``paddle.nn.Layer`` 的forward方法的所有返回变量,
将会依据输入的 ``output_spec`` 列表对存储的模型进行裁剪。

.. note::
Expand Down Expand Up @@ -132,7 +132,7 @@ SaveLoadConfig
.. py:attribute:: model_filename
存储转写 :ref:`cn_api_fluid_dygraph_Layer` 模型结构 ``Program`` 的文件名称。默认文件名为 ``__model__``。
存储转写 ``paddle.nn.Layer`` 模型结构 ``Program`` 的文件名称。默认文件名为 ``__model__``。

**示例代码**
.. code-block:: python
Expand Down Expand Up @@ -183,7 +183,7 @@ SaveLoadConfig
.. py:attribute:: params_filename
存储转写 :ref:`cn_api_fluid_dygraph_Layer` 所有持久参数(包括 ``Parameters`` 和持久的 ``Buffers``)的文件名称。默认文件名称为 ``__variable__``。
存储转写 ``paddle.nn.Layer`` 所有持久参数(包括 ``Parameters`` 和持久的 ``Buffers``)的文件名称。默认文件名称为 ``__variable__``。

**示例代码**
.. code-block:: python
Expand Down Expand Up @@ -236,10 +236,13 @@ SaveLoadConfig
.. py:attribute:: separate_params
配置是否将 :ref:`cn_api_fluid_dygraph_Layer` 的参数存储为分散的文件。
(这是为了兼容接口 :ref:`cn_api_fluid_io_save_inference_model` 的行为)
(这是为了兼容接口 ``paddle.static.save_inference_model`` 的行为)

如果设置为 ``True`` ,每个参数将会被存储为一个文件,文件名为参数名,同时``SaveLoadConfig.params_filename`` 指定的文件名将不会生效。默认为 ``False``。

.. note::
仅用于 ``paddle.load`` 方法.

**示例代码**
.. code-block:: python
Expand Down Expand Up @@ -273,7 +276,7 @@ SaveLoadConfig
adam.clear_grad()
model_path = "simplenet.example.model.separate_params"
config = paddle.jit.SaveLoadConfig()
config = paddle.SaveLoadConfig()
config.separate_params = True
# saving with configs.separate_params
Expand Down Expand Up @@ -307,11 +310,11 @@ SaveLoadConfig
linear = paddle.nn.Linear(5, 1)
state_dict = linear.state_dict()
paddle.save(state_dict, "paddle_dy")
paddle.save(state_dict, "paddle_dy.pdparams")
configs = paddle.SaveLoadConfig()
configs.keep_name_table = True
para_state_dict, _ = paddle.load("paddle_dy", configs)
config = paddle.SaveLoadConfig()
config.keep_name_table = True
para_state_dict = paddle.load("paddle_dy.pdparams", config)
print(para_state_dict)
# the name_table is 'StructuredToParameterName@@'
Expand Down
6 changes: 3 additions & 3 deletions doc/paddle/api/paddle/fluid/dygraph/layers/Layer_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ buffer是一个非参数类型的变量,不会被优化器更新,但在评
paddle.disable_static()
emb = paddle.nn.Embedding([10, 10])
emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict()
paddle.save(state_dict, "paddle_dy")
paddle.save(state_dict, "paddle_dy.pdparams")
para_state_dict, _ = paddle.load("paddle_dy")
para_state_dict = paddle.load("paddle_dy.pdparams")
emb.set_state_dict(para_state_dict)
Loading

0 comments on commit 4f6c833

Please sign in to comment.