未验证 提交 7ff66973 编写于 作者: G GGBond8488 提交者: GitHub

Move out sequential and replace save_dygraph and load_dygraph (#48709)

* remove fluid.save_dygraph and fluid.load_dygraph use paddle.save and paddle.load instead

* move Sequential to paddle.nn

* modify convert_call_func.py Sequential reference

* remove related unitests

* remove fluid.dynamic.Sequntial

* test remove conver_call_func

* fix conflicts

* fix typro

* fix unitests

* fix sample_code

* fix unitest

* fix __init__
上级 42fb43e7
...@@ -101,7 +101,6 @@ from . import install_check ...@@ -101,7 +101,6 @@ from . import install_check
from .dygraph.layers import * from .dygraph.layers import *
from .dygraph.base import enable_dygraph, disable_dygraph from .dygraph.base import enable_dygraph, disable_dygraph
from .io import save, load, load_program_state, set_program_state from .io import save, load, load_program_state, set_program_state
from .dygraph.checkpoint import save_dygraph, load_dygraph
from .dygraph.varbase_patch_methods import monkey_patch_varbase from .dygraph.varbase_patch_methods import monkey_patch_varbase
from . import generator from . import generator
from .core import _cuda_synchronize from .core import _cuda_synchronize
......
...@@ -18,17 +18,12 @@ from .base import * ...@@ -18,17 +18,12 @@ from .base import *
from . import layers from . import layers
from .layers import * from .layers import *
from . import container
from .container import *
from . import tracer from . import tracer
from .tracer import * from .tracer import *
from . import parallel from . import parallel
from .parallel import * from .parallel import *
from . import checkpoint
from .checkpoint import *
from . import learning_rate_scheduler from . import learning_rate_scheduler
from .learning_rate_scheduler import * from .learning_rate_scheduler import *
...@@ -41,8 +36,6 @@ from .math_op_patch import monkey_patch_math_varbase ...@@ -41,8 +36,6 @@ from .math_op_patch import monkey_patch_math_varbase
__all__ = [] __all__ = []
__all__ += layers.__all__ __all__ += layers.__all__
__all__ += base.__all__ __all__ += base.__all__
__all__ += container.__all__
__all__ += parallel.__all__ __all__ += parallel.__all__
__all__ += checkpoint.__all__
__all__ += learning_rate_scheduler.__all__ __all__ += learning_rate_scheduler.__all__
__all__ += amp.__all__ __all__ += amp.__all__
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import collections
import functools
from ..framework import (
Variable,
default_main_program,
dygraph_only,
Parameter,
ParamBase,
_varbase_creator,
_dygraph_tracer,
EagerParamBase,
)
import pickle
from . import learning_rate_scheduler
import warnings
from .. import core
from .base import guard
from paddle.jit.api import _SaveLoadConfig
from paddle.jit.translated_layer import (
_construct_program_holders,
_construct_params_and_buffers,
)
__all__ = [
'save_dygraph',
'load_dygraph',
]
def _parse_load_config(configs):
supported_configs = ['model_filename', 'params_filename', 'keep_name_table']
# input check
for key in configs:
if key not in supported_configs:
raise ValueError(
"The additional config (%s) of `paddle.fluid.load_dygraph` is not supported."
% (key)
)
# construct inner config
inner_config = _SaveLoadConfig()
inner_config.model_filename = configs.get('model_filename', None)
inner_config.params_filename = configs.get('params_filename', None)
inner_config.keep_name_table = configs.get('keep_name_table', None)
return inner_config
@dygraph_only
def save_dygraph(state_dict, model_path):
'''
:api_attr: imperative
Save Layer's state_dict to disk. This will generate a file with suffix ".pdparams"
The state_dict is get from Layers.state_dict function
Args:
state_dict(dict) : The state dict to be saved.
model_path(str) : the file prefix to save the state_dict. The format is "dirname/file_prefix". If file_prefix is empty str. A exception will be raised
Returns:
None
Examples:
.. code-block:: python
import paddle.fluid as fluid
import paddle
with fluid.dygraph.guard():
emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict()
fluid.save_dygraph( state_dict, "paddle_dy")
adam = fluid.optimizer.Adam( learning_rate = fluid.layers.noam_decay( 100, 10000),
parameter_list = emb.parameters() )
state_dict = adam.state_dict()
fluid.save_dygraph( state_dict, "paddle_dy")
'''
base_name = os.path.basename(model_path)
assert (
base_name != ""
), "The input model_path MUST be format of dirname/filename [dirname\\filename in Windows system], but received filename is empty string."
suffix = ".pdparams"
assert len(state_dict) > 0, "state_dict is empty, no need to save"
param_num = 0
for k, v in state_dict.items():
if isinstance(v, (ParamBase, EagerParamBase)):
param_num += 1
if param_num == 0:
suffix = ".pdopt"
model_dict = {}
name_table = {}
for k, v in state_dict.items():
if isinstance(v, (Variable, core.VarBase, core.eager.Tensor)):
model_dict[k] = v.numpy()
name_table[k] = v.name
else:
model_dict[k] = v
model_dict["StructuredToParameterName@@"] = name_table
file_name = model_path + suffix
dir_name = os.path.dirname(file_name)
if dir_name and not os.path.exists(dir_name):
os.makedirs(dir_name)
with open(file_name, 'wb') as f:
pickle.dump(model_dict, f, protocol=2)
# NOTE(chenweihang): load_dygraph will deprecated in future, we don't
# support new loading features for it
# TODO(qingqing01): remove dygraph_only to support loading static model.
# maybe need to unify the loading interface after 2.0 API is ready.
# @dygraph_only
def load_dygraph(model_path, **configs):
'''
:api_attr: imperative
Load parameter state dict from disk.
.. note::
Due to some historical reasons, if you load ``state_dict`` from the saved
result of `paddle.static.save_inference_model`, the structured variable name
will cannot be restored. You need to set the argument `use_structured_name=False`
when using `Layer.set_state_dict` later.
Args:
model_path(str) : The file prefix store the state_dict.
(The path should Not contain suffix '.pdparams')
**configs (dict, optional): Other load configuration options for compatibility. We do not
recommend using these configurations, if not necessary, DO NOT use them. Default None.
The following options are currently supported:
(1) model_filename (str): The inference model file name of the paddle 1.x ``save_inference_model``
save format. Default file name is :code:`__model__` .
(2) params_filename (str): The persistable variables file name of the paddle 1.x ``save_inference_model``
save format. No default file name, save variables separately by default.
Returns:
state_dict(dict) : the dict store the state_dict
Examples:
.. code-block:: python
import paddle
import paddle.fluid as fluid
paddle.disable_static()
emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy")
scheduler = paddle.optimizer.lr.NoamDecay(
d_model=0.01, warmup_steps=100, verbose=True)
adam = paddle.optimizer.Adam(
learning_rate=scheduler,
parameters=emb.parameters())
state_dict = adam.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy")
para_state_dict, opti_state_dict = fluid.load_dygraph("paddle_dy")
'''
# deal with argument `model_path`
model_prefix = model_path
if model_prefix.endswith(".pdparams"):
model_prefix = model_prefix[:-9]
elif model_prefix.endswith(".pdopt"):
model_prefix = model_prefix[:-6]
para_dict = None
opti_dict = None
params_file_path = model_prefix + ".pdparams"
opti_file_path = model_prefix + ".pdopt"
# deal with argument `config`
config = _parse_load_config(configs)
if os.path.exists(params_file_path) or os.path.exists(opti_file_path):
# Load state dict by `save_dygraph` save format
para_dict = {}
if os.path.exists(params_file_path):
with open(params_file_path, 'rb') as f:
para_dict = pickle.load(f, encoding='latin1')
if (
not config.keep_name_table
and "StructuredToParameterName@@" in para_dict
):
del para_dict["StructuredToParameterName@@"]
if os.path.exists(opti_file_path):
with open(opti_file_path, 'rb') as f:
opti_dict = pickle.load(f, encoding='latin1')
else:
# check model path
if not os.path.isdir(model_prefix):
raise ValueError(
"Model saved directory '%s' is not exists." % model_prefix
)
# check whether model file exists
if config.model_filename is None:
model_filename = '__model__'
else:
model_filename = config.model_filename
model_file_path = os.path.join(model_path, model_filename)
if os.path.exists(model_file_path):
# Load state dict by `jit.save/io.save_inference_model` save format
# NOTE(chenweihang): [ Compatibility of save_inference_model save format ]
# The model saved by `save_inference_model` does not completely correspond to
# the information required by the `state_dict` under the dygraph.
# `save_inference_model` not save structured name, we need to remind
# the user to configure the `use_structured_name` argument when `set_state_dict`
# NOTE(chenweihang): `jit.save` doesn't save optimizer state
# 1. load program desc & construct _ProgramHolder
programs = _construct_program_holders(
model_path, config.model_filename
)
# 2. load layer parameters & buffers
with guard():
persistable_var_dict = _construct_params_and_buffers(
model_prefix,
programs,
config.params_filename,
append_suffix=False,
)
# 3. construct state_dict
para_dict = dict()
for var_name in persistable_var_dict:
para_dict[var_name] = persistable_var_dict[var_name].numpy()
# if *.info exists, we can recover structured_name
var_info_filename = str(config.params_filename) + ".info"
var_info_path = os.path.join(model_prefix, var_info_filename)
if os.path.exists(var_info_path):
with open(var_info_path, 'rb') as f:
extra_var_info = pickle.load(f)
structured_para_dict = dict()
for var_name in para_dict:
structured_name = extra_var_info[var_name].get(
'structured_name', None
)
assert structured_name is not None, (
"Cannot find saved variable (%s)'s structured name in saved model."
% var_name
)
structured_para_dict[structured_name] = para_dict[
var_name
]
para_dict = structured_para_dict
else:
# load state dict by `io.save_params/persistables` save format
# TODO(chenweihang): [ Now only supports loading parameters separately ]
# If users save all parameters as one file, the [ variable.name -> variable ]
# mapping info will lost, so users need to give variable list, but users build
# variable list in dygraph mode is difficult, we recommend users to use
# paddle.static.load_program_state in this case
# Try to load all the files in the directory in VarBase format,
# the file name is used as the name of VarBase
load_var_list = []
# 1. load file names
var_name_list = []
for root, _, files in os.walk(model_path):
for filename in files:
file_path = os.path.join(root, filename)
tmp_var_name = os.path.relpath(file_path, model_path)
var_name = tmp_var_name.replace("\\", "/")
var_name_list.append(var_name)
# 2. create and load VarBase
with guard():
for name in var_name_list:
new_var = _varbase_creator(name=name, persistable=True)
_dygraph_tracer().trace_op(
type='load',
inputs={},
outputs={'Out': new_var},
attrs={'file_path': os.path.join(model_path, name)},
)
load_var_list.append(new_var)
# 3. construct state_dict
para_dict = dict()
for var in load_var_list:
para_dict[var.name] = var.numpy()
return para_dict, opti_dict
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .layers import Layer
__all__ = [
'Sequential',
]
class Sequential(Layer):
"""Sequential container.
Sub layers will be added to this container in the order of argument in the constructor.
The argument passed to the constructor can be iterable Layers or iterable name Layer pairs.
Parameters:
layers(Layer|list|tuple): Layer or list/tuple of iterable name Layer pair.
Examples:
.. code-block:: python
import paddle
import numpy as np
data = np.random.uniform(-1, 1, [30, 10]).astype('float32')
data = paddle.to_tensor(data)
# create Sequential with iterable Layers
model1 = paddle.nn.Sequential(
paddle.nn.Linear(10, 1), paddle.nn.Linear(1, 2)
)
model1[0] # access the first layer
res1 = model1(data) # sequential execution
# create Sequential with name Layer pairs
model2 = paddle.nn.Sequential(
('l1', paddle.nn.Linear(10, 2)),
('l2', paddle.nn.Linear(2, 3))
)
model2['l1'] # access l1 layer
model2.add_sublayer('l3', paddle.nn.Linear(3, 3)) # add sublayer
res2 = model2(data) # sequential execution
"""
def __init__(self, *layers):
super().__init__()
if len(layers) > 0 and isinstance(layers[0], (list, tuple)):
for name, layer in layers:
self.add_sublayer(name, layer)
else:
for idx, layer in enumerate(layers):
self.add_sublayer(str(idx), layer)
def __getitem__(self, name):
if isinstance(name, slice):
return self.__class__(*(list(self._sub_layers.values())[name]))
elif isinstance(name, str):
return self._sub_layers[name]
else:
if name >= len(self._sub_layers):
raise IndexError('index {} is out of range'.format(name))
elif name < 0 and name >= -len(self._sub_layers):
name += len(self._sub_layers)
elif name < -len(self._sub_layers):
raise IndexError('index {} is out of range'.format(name))
return list(self._sub_layers.values())[name]
def __setitem__(self, name, layer):
assert isinstance(layer, Layer)
setattr(self, str(name), layer)
def __delitem__(self, name):
name = str(name)
assert name in self._sub_layers
del self._sub_layers[name]
def __len__(self):
return len(self._sub_layers)
def forward(self, input):
for layer in self._sub_layers.values():
input = layer(input)
return input
...@@ -257,14 +257,13 @@ class Optimizer: ...@@ -257,14 +257,13 @@ class Optimizer:
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.fluid as fluid
paddle.disable_static() paddle.disable_static()
emb = paddle.nn.Embedding(10, 10) emb = paddle.nn.Embedding(10, 10)
state_dict = emb.state_dict() state_dict = emb.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy") paddle.save(state_dict, "paddle_dy.pdparams")
scheduler = paddle.optimizer.lr.NoamDecay( scheduler = paddle.optimizer.lr.NoamDecay(
d_model=0.01, warmup_steps=100, verbose=True) d_model=0.01, warmup_steps=100, verbose=True)
...@@ -272,9 +271,10 @@ class Optimizer: ...@@ -272,9 +271,10 @@ class Optimizer:
learning_rate=scheduler, learning_rate=scheduler,
parameters=emb.parameters()) parameters=emb.parameters())
state_dict = adam.state_dict() state_dict = adam.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy") paddle.save(state_dict, "paddle_dy.pdopt")
para_state_dict, opti_state_dict = fluid.load_dygraph("paddle_dy") para_state_dict = paddle.load("paddle_dy.pdparams")
opti_state_dict = paddle.load("paddle_dy.pdopt")
''' '''
from paddle.optimizer.lr import LRScheduler from paddle.optimizer.lr import LRScheduler
......
...@@ -1037,7 +1037,6 @@ set_tests_properties(test_index_add_op PROPERTIES TIMEOUT 120) ...@@ -1037,7 +1037,6 @@ set_tests_properties(test_index_add_op PROPERTIES TIMEOUT 120)
set_tests_properties(test_parallel_ssa_graph_inference_feed_partial_data set_tests_properties(test_parallel_ssa_graph_inference_feed_partial_data
PROPERTIES TIMEOUT 120) PROPERTIES TIMEOUT 120)
set_tests_properties(test_tensordot PROPERTIES TIMEOUT 200) set_tests_properties(test_tensordot PROPERTIES TIMEOUT 200)
set_tests_properties(test_imperative_save_load PROPERTIES TIMEOUT 120)
set_tests_properties(test_partial_eager_deletion_transformer PROPERTIES TIMEOUT set_tests_properties(test_partial_eager_deletion_transformer PROPERTIES TIMEOUT
120) 120)
set_tests_properties(test_parallel_executor_seresnext_with_reduce_gpu set_tests_properties(test_parallel_executor_seresnext_with_reduce_gpu
......
...@@ -119,8 +119,9 @@ class TestBert(unittest.TestCase): ...@@ -119,8 +119,9 @@ class TestBert(unittest.TestCase):
if to_static: if to_static:
paddle.jit.save(bert, self.model_save_prefix) paddle.jit.save(bert, self.model_save_prefix)
else: else:
fluid.dygraph.save_dygraph( paddle.save(
bert.state_dict(), self.dy_state_dict_save_path bert.state_dict(),
self.dy_state_dict_save_path + '.pdparams',
) )
break break
return loss, ppl return loss, ppl
...@@ -161,9 +162,7 @@ class TestBert(unittest.TestCase): ...@@ -161,9 +162,7 @@ class TestBert(unittest.TestCase):
bert = PretrainModelLayer( bert = PretrainModelLayer(
config=bert_config, weight_sharing=False, use_fp16=False config=bert_config, weight_sharing=False, use_fp16=False
) )
model_dict, _ = fluid.dygraph.load_dygraph( model_dict = paddle.load(self.dy_state_dict_save_path + '.pdparams')
self.dy_state_dict_save_path
)
bert.set_dict(model_dict) bert.set_dict(model_dict)
bert.eval() bert.eval()
......
...@@ -747,8 +747,9 @@ class TestTrain(unittest.TestCase): ...@@ -747,8 +747,9 @@ class TestTrain(unittest.TestCase):
if to_static: if to_static:
paddle.jit.save(bmn, self.model_save_prefix) paddle.jit.save(bmn, self.model_save_prefix)
else: else:
fluid.dygraph.save_dygraph( paddle.save(
bmn.state_dict(), self.dy_param_path bmn.state_dict(),
self.dy_param_path + '.pdparams',
) )
break break
return np.array(loss_data) return np.array(loss_data)
...@@ -825,7 +826,7 @@ class TestTrain(unittest.TestCase): ...@@ -825,7 +826,7 @@ class TestTrain(unittest.TestCase):
with fluid.dygraph.guard(self.place): with fluid.dygraph.guard(self.place):
bmn = BMN(self.args) bmn = BMN(self.args)
# load dygraph trained parameters # load dygraph trained parameters
model_dict, _ = fluid.load_dygraph(self.dy_param_path + ".pdparams") model_dict = paddle.load(self.dy_param_path + ".pdparams")
bmn.set_dict(model_dict) bmn.set_dict(model_dict)
bmn.eval() bmn.eval()
......
...@@ -622,8 +622,8 @@ class TestLACModel(unittest.TestCase): ...@@ -622,8 +622,8 @@ class TestLACModel(unittest.TestCase):
output_spec=[crf_decode], output_spec=[crf_decode],
) )
else: else:
fluid.dygraph.save_dygraph( paddle.save(
model.state_dict(), self.dy_param_path model.state_dict(), self.dy_param_path + '.pdparams'
) )
return np.array(loss_data) return np.array(loss_data)
...@@ -660,7 +660,7 @@ class TestLACModel(unittest.TestCase): ...@@ -660,7 +660,7 @@ class TestLACModel(unittest.TestCase):
with fluid.dygraph.guard(self.place): with fluid.dygraph.guard(self.place):
model = LexNet(self.args) model = LexNet(self.args)
# load dygraph trained parameters # load dygraph trained parameters
model_dict, _ = fluid.load_dygraph(self.dy_param_path + ".pdparams") model_dict = paddle.load(self.dy_param_path + ".pdparams")
model.set_dict(model_dict) model.set_dict(model_dict)
model.eval() model.eval()
......
...@@ -571,8 +571,9 @@ def train_mobilenet(args, to_static): ...@@ -571,8 +571,9 @@ def train_mobilenet(args, to_static):
if to_static: if to_static:
paddle.jit.save(net, args.model_save_prefix) paddle.jit.save(net, args.model_save_prefix)
else: else:
fluid.dygraph.save_dygraph( paddle.save(
net.state_dict(), args.dy_state_dict_save_path net.state_dict(),
args.dy_state_dict_save_path + '.pdparams',
) )
break break
...@@ -611,7 +612,7 @@ def predict_dygraph(args, data): ...@@ -611,7 +612,7 @@ def predict_dygraph(args, data):
elif args.model == "MobileNetV2": elif args.model == "MobileNetV2":
model = MobileNetV2(class_dim=args.class_dim, scale=1.0) model = MobileNetV2(class_dim=args.class_dim, scale=1.0)
# load dygraph trained parameters # load dygraph trained parameters
model_dict, _ = fluid.load_dygraph(args.dy_state_dict_save_path) model_dict = paddle.load(args.dy_state_dict_save_path + '.pdparams')
model.set_dict(model_dict) model.set_dict(model_dict)
model.eval() model.eval()
......
...@@ -312,9 +312,9 @@ class ResNetHelper: ...@@ -312,9 +312,9 @@ class ResNetHelper:
if to_static: if to_static:
paddle.jit.save(resnet, self.model_save_prefix) paddle.jit.save(resnet, self.model_save_prefix)
else: else:
fluid.dygraph.save_dygraph( paddle.save(
resnet.state_dict(), resnet.state_dict(),
self.dy_state_dict_save_path, self.dy_state_dict_save_path + '.pdparams',
) )
# avoid dataloader throw abort signaal # avoid dataloader throw abort signaal
data_loader._reset() data_loader._reset()
...@@ -327,9 +327,7 @@ class ResNetHelper: ...@@ -327,9 +327,7 @@ class ResNetHelper:
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
resnet = ResNet() resnet = ResNet()
model_dict, _ = fluid.dygraph.load_dygraph( model_dict = paddle.load(self.dy_state_dict_save_path + '.pdparams')
self.dy_state_dict_save_path
)
resnet.set_dict(model_dict) resnet.set_dict(model_dict)
resnet.eval() resnet.eval()
......
...@@ -307,8 +307,9 @@ class TestResnet(unittest.TestCase): ...@@ -307,8 +307,9 @@ class TestResnet(unittest.TestCase):
if to_static: if to_static:
paddle.jit.save(resnet, self.model_save_prefix) paddle.jit.save(resnet, self.model_save_prefix)
else: else:
paddle.fluid.dygraph.save_dygraph( paddle.save(
resnet.state_dict(), self.dy_state_dict_save_path resnet.state_dict(),
self.dy_state_dict_save_path + '.pdparams',
) )
# avoid dataloader throw abort signaal # avoid dataloader throw abort signaal
data_loader._reset() data_loader._reset()
...@@ -322,9 +323,7 @@ class TestResnet(unittest.TestCase): ...@@ -322,9 +323,7 @@ class TestResnet(unittest.TestCase):
paddle.disable_static(place) paddle.disable_static(place)
resnet = ResNet() resnet = ResNet()
model_dict, _ = paddle.fluid.dygraph.load_dygraph( model_dict = paddle.load(self.dy_state_dict_save_path + '.pdparams')
self.dy_state_dict_save_path
)
resnet.set_dict(model_dict) resnet.set_dict(model_dict)
resnet.eval() resnet.eval()
......
...@@ -19,6 +19,7 @@ import unittest ...@@ -19,6 +19,7 @@ import unittest
import numpy as np import numpy as np
from test_fetch_feed import Linear from test_fetch_feed import Linear
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.optimizer import AdamOptimizer from paddle.fluid.optimizer import AdamOptimizer
from paddle.jit import ProgramTranslator from paddle.jit import ProgramTranslator
...@@ -62,7 +63,7 @@ class TestDyToStaticSaveLoad(unittest.TestCase): ...@@ -62,7 +63,7 @@ class TestDyToStaticSaveLoad(unittest.TestCase):
net.clear_gradients() net.clear_gradients()
# Save parameters # Save parameters
fluid.save_dygraph(net.state_dict(), self.model_path) paddle.save(net.state_dict(), self.model_path + '.pdparams')
# minimize() will update parameter, call net() to get output and avg_loss. # minimize() will update parameter, call net() to get output and avg_loss.
# Switch into eval mode. # Switch into eval mode.
net.eval() net.eval()
...@@ -73,7 +74,7 @@ class TestDyToStaticSaveLoad(unittest.TestCase): ...@@ -73,7 +74,7 @@ class TestDyToStaticSaveLoad(unittest.TestCase):
dygraph_net = Linear(32, 64) dygraph_net = Linear(32, 64)
# Load parameters # Load parameters
model_dict, _ = fluid.load_dygraph(self.model_path) model_dict = paddle.load(self.model_path + '.pdparams')
dygraph_net.set_dict(model_dict) dygraph_net.set_dict(model_dict)
# Switch into eval mode. # Switch into eval mode.
dygraph_net.eval() dygraph_net.eval()
......
...@@ -460,9 +460,9 @@ class TestSeResnet(unittest.TestCase): ...@@ -460,9 +460,9 @@ class TestSeResnet(unittest.TestCase):
output_spec=[pred], output_spec=[pred],
) )
else: else:
fluid.dygraph.save_dygraph( paddle.save(
se_resnext.state_dict(), se_resnext.state_dict(),
self.dy_state_dict_save_path, self.dy_state_dict_save_path + '.pdparams',
) )
break break
return ( return (
...@@ -478,9 +478,7 @@ class TestSeResnet(unittest.TestCase): ...@@ -478,9 +478,7 @@ class TestSeResnet(unittest.TestCase):
with fluid.dygraph.guard(place): with fluid.dygraph.guard(place):
se_resnext = SeResNeXt() se_resnext = SeResNeXt()
model_dict, _ = fluid.dygraph.load_dygraph( model_dict = paddle.load(self.dy_state_dict_save_path + '.pdparams')
self.dy_state_dict_save_path
)
se_resnext.set_dict(model_dict) se_resnext.set_dict(model_dict)
se_resnext.eval() se_resnext.eval()
......
...@@ -21,6 +21,7 @@ import numpy as np ...@@ -21,6 +21,7 @@ import numpy as np
from seq2seq_dygraph_model import AttentionModel, BaseModel from seq2seq_dygraph_model import AttentionModel, BaseModel
from seq2seq_utils import Seq2SeqModelHyperParams, get_data_iter from seq2seq_utils import Seq2SeqModelHyperParams, get_data_iter
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.jit import ProgramTranslator from paddle.jit import ProgramTranslator
from paddle.nn import ClipGradByGlobalNorm from paddle.nn import ClipGradByGlobalNorm
...@@ -128,7 +129,7 @@ def train(args, attn_model=False): ...@@ -128,7 +129,7 @@ def train(args, attn_model=False):
if not os.path.exists(model_dir): if not os.path.exists(model_dir):
os.makedirs(model_dir) os.makedirs(model_dir)
fluid.save_dygraph(model.state_dict(), model_dir) paddle.save(model.state_dict(), model_dir + '.pdparams')
return loss.numpy() return loss.numpy()
...@@ -163,7 +164,7 @@ def infer(args, attn_model=False): ...@@ -163,7 +164,7 @@ def infer(args, attn_model=False):
model_path = ( model_path = (
args.attn_model_path if attn_model else args.base_model_path args.attn_model_path if attn_model else args.base_model_path
) )
state_dict, _ = fluid.dygraph.load_dygraph(model_path) state_dict = paddle.load(model_path + '.pdparams')
model.set_dict(state_dict) model.set_dict(state_dict)
model.eval() model.eval()
train_data_iter = get_data_iter(args.batch_size, mode='infer') train_data_iter = get_data_iter(args.batch_size, mode='infer')
......
...@@ -301,13 +301,15 @@ def train_dygraph(args, batch_generator): ...@@ -301,13 +301,15 @@ def train_dygraph(args, batch_generator):
model_dir = os.path.join(args.save_dygraph_model_path) model_dir = os.path.join(args.save_dygraph_model_path)
if not os.path.exists(model_dir): if not os.path.exists(model_dir):
os.makedirs(model_dir) os.makedirs(model_dir)
fluid.save_dygraph( paddle.save(
transformer.state_dict(), transformer.state_dict(),
os.path.join(model_dir, "transformer"), os.path.join(model_dir, "transformer")
+ '.pdparams',
) )
fluid.save_dygraph( paddle.save(
optimizer.state_dict(), optimizer.state_dict(),
os.path.join(model_dir, "transformer"), os.path.join(model_dir, "transformer")
+ '.pdparams',
) )
break break
time_consumed = time.time() - pass_start_time time_consumed = time.time() - pass_start_time
......
...@@ -329,8 +329,11 @@ def load_dygraph(model_path, keep_name_table=False): ...@@ -329,8 +329,11 @@ def load_dygraph(model_path, keep_name_table=False):
To load python2 saved models in python3. To load python2 saved models in python3.
""" """
try: try:
para_dict, opti_dict = fluid.load_dygraph( para_dict = paddle.load(
model_path, keep_name_table=keep_name_table model_path + '.pdparams', keep_name_table=keep_name_table
)
opti_dict = paddle.load(
model_path + '.pdopt', keep_name_table=keep_name_table
) )
return para_dict, opti_dict return para_dict, opti_dict
except UnicodeDecodeError: except UnicodeDecodeError:
...@@ -341,8 +344,11 @@ def load_dygraph(model_path, keep_name_table=False): ...@@ -341,8 +344,11 @@ def load_dygraph(model_path, keep_name_table=False):
) )
load_bak = pickle.load load_bak = pickle.load
pickle.load = partial(load_bak, encoding="latin1") pickle.load = partial(load_bak, encoding="latin1")
para_dict, opti_dict = fluid.load_dygraph( para_dict = paddle.load(
model_path, keep_name_table=keep_name_table model_path + '.pdparams', keep_name_table=keep_name_table
)
opti_dict = paddle.load(
model_path + '.pdopt', keep_name_table=keep_name_table
) )
pickle.load = load_bak pickle.load = load_bak
return para_dict, opti_dict return para_dict, opti_dict
...@@ -983,7 +983,7 @@ class TestAdamOptimizer(unittest.TestCase): ...@@ -983,7 +983,7 @@ class TestAdamOptimizer(unittest.TestCase):
linear = paddle.nn.Linear(10, 10) linear = paddle.nn.Linear(10, 10)
b = linear(a) b = linear(a)
state_dict = linear.state_dict() state_dict = linear.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy") paddle.save(state_dict, "paddle_dy.pdparams")
scheduler = paddle.optimizer.lr.NoamDecay( scheduler = paddle.optimizer.lr.NoamDecay(
d_model=0.01, warmup_steps=100, verbose=True d_model=0.01, warmup_steps=100, verbose=True
...@@ -995,8 +995,9 @@ class TestAdamOptimizer(unittest.TestCase): ...@@ -995,8 +995,9 @@ class TestAdamOptimizer(unittest.TestCase):
) )
adam.minimize(b) adam.minimize(b)
state_dict = adam.state_dict() state_dict = adam.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy") paddle.save(state_dict, "paddle_dy.pdopt")
para_state_dict, opt_state_dict = fluid.load_dygraph("paddle_dy") para_state_dict = paddle.load("paddle_dy.pdparams")
opt_state_dict = paddle.load("paddle_dy.pdopt")
adam.set_state_dict(opt_state_dict) adam.set_state_dict(opt_state_dict)
paddle.enable_static() paddle.enable_static()
...@@ -1011,7 +1012,7 @@ class TestAdamOptimizer(unittest.TestCase): ...@@ -1011,7 +1012,7 @@ class TestAdamOptimizer(unittest.TestCase):
linear = paddle.nn.Linear(10, 10) linear = paddle.nn.Linear(10, 10)
b = linear(a) b = linear(a)
state_dict = linear.state_dict() state_dict = linear.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy") paddle.save(state_dict, "paddle_dy.pdparams")
scheduler = paddle.optimizer.lr.NoamDecay( scheduler = paddle.optimizer.lr.NoamDecay(
d_model=0.01, warmup_steps=100, verbose=True d_model=0.01, warmup_steps=100, verbose=True
...@@ -1027,8 +1028,9 @@ class TestAdamOptimizer(unittest.TestCase): ...@@ -1027,8 +1028,9 @@ class TestAdamOptimizer(unittest.TestCase):
adam = get_opt('float32', [10, 10]) adam = get_opt('float32', [10, 10])
state_dict = adam.state_dict() state_dict = adam.state_dict()
fluid.save_dygraph(state_dict, "paddle_dy") paddle.save(state_dict, "paddle_dy.pdopt")
para_state_dict, opt_state_dict = fluid.load_dygraph("paddle_dy") para_state_dict = paddle.load("paddle_dy.pdparams")
opt_state_dict = paddle.load("paddle_dy.pdopt")
adam.set_state_dict(opt_state_dict) adam.set_state_dict(opt_state_dict)
adam2 = get_opt('float64', [10, 10]) # dtype not match adam2 = get_opt('float64', [10, 10]) # dtype not match
......
...@@ -26,7 +26,7 @@ class TestImperativeContainerSequential(unittest.TestCase): ...@@ -26,7 +26,7 @@ class TestImperativeContainerSequential(unittest.TestCase):
data = np.random.uniform(-1, 1, [5, 10]).astype('float32') data = np.random.uniform(-1, 1, [5, 10]).astype('float32')
with fluid.dygraph.guard(): with fluid.dygraph.guard():
data = fluid.dygraph.to_variable(data) data = fluid.dygraph.to_variable(data)
model1 = fluid.dygraph.Sequential(Linear(10, 1), Linear(1, 2)) model1 = paddle.nn.Sequential(Linear(10, 1), Linear(1, 2))
res1 = model1(data) res1 = model1(data)
self.assertListEqual(res1.shape, [5, 2]) self.assertListEqual(res1.shape, [5, 2])
model1[1] = Linear(1, 3) model1[1] = Linear(1, 3)
...@@ -37,7 +37,7 @@ class TestImperativeContainerSequential(unittest.TestCase): ...@@ -37,7 +37,7 @@ class TestImperativeContainerSequential(unittest.TestCase):
l1 = Linear(10, 1) l1 = Linear(10, 1)
l2 = Linear(1, 3) l2 = Linear(1, 3)
model2 = fluid.dygraph.Sequential(('l1', l1), ('l2', l2)) model2 = paddle.nn.Sequential(('l1', l1), ('l2', l2))
self.assertEqual(len(model2), 2) self.assertEqual(len(model2), 2)
res2 = model2(data) res2 = model2(data)
self.assertTrue(l1 is model2.l1) self.assertTrue(l1 is model2.l1)
...@@ -60,7 +60,7 @@ class TestImperativeContainerSequential(unittest.TestCase): ...@@ -60,7 +60,7 @@ class TestImperativeContainerSequential(unittest.TestCase):
data = np.random.uniform(-1, 1, [5, 10]).astype('float32') data = np.random.uniform(-1, 1, [5, 10]).astype('float32')
with fluid.dygraph.guard(): with fluid.dygraph.guard():
data = fluid.dygraph.to_variable(data) data = fluid.dygraph.to_variable(data)
model1 = fluid.dygraph.Sequential(Linear(10, 1), Linear(1, 2)) model1 = paddle.nn.Sequential(Linear(10, 1), Linear(1, 2))
res1 = model1(data) res1 = model1(data)
self.assertListEqual(res1.shape, [5, 2]) self.assertListEqual(res1.shape, [5, 2])
model1[1] = Linear(1, 3) model1[1] = Linear(1, 3)
...@@ -71,7 +71,7 @@ class TestImperativeContainerSequential(unittest.TestCase): ...@@ -71,7 +71,7 @@ class TestImperativeContainerSequential(unittest.TestCase):
l1 = Linear(10, 1) l1 = Linear(10, 1)
l2 = Linear(1, 3) l2 = Linear(1, 3)
model2 = fluid.dygraph.Sequential(['l1', l1], ['l2', l2]) model2 = paddle.nn.Sequential(['l1', l1], ['l2', l2])
self.assertEqual(len(model2), 2) self.assertEqual(len(model2), 2)
res2 = model2(data) res2 = model2(data)
self.assertTrue(l1 is model2.l1) self.assertTrue(l1 is model2.l1)
......
...@@ -844,9 +844,9 @@ class TestDygraphPtbRnn(unittest.TestCase): ...@@ -844,9 +844,9 @@ class TestDygraphPtbRnn(unittest.TestCase):
last_hidden = None last_hidden = None
last_cell = None last_cell = None
state_dict, opti_dict = fluid.load_dygraph( model_prefix = os.path.join(self.temp_dir.name, "test_dy_v2")
os.path.join(self.temp_dir.name, "test_dy_v2") state_dict = paddle.load(model_prefix + '.pdparams')
) opti_dict = paddle.load(model_prefix + '.pdopt')
adam.set_state_dict(opti_dict) adam.set_state_dict(opti_dict)
ptb_model.set_dict(state_dict) ptb_model.set_dict(state_dict)
......
...@@ -430,7 +430,7 @@ class TestJitSaveLoad(unittest.TestCase): ...@@ -430,7 +430,7 @@ class TestJitSaveLoad(unittest.TestCase):
self.temp_dir.name, "test_jit_save_load.no_path/model_path" self.temp_dir.name, "test_jit_save_load.no_path/model_path"
) )
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
model_dict, _ = fluid.dygraph.load_dygraph(model_path) model_dict = paddle.load(model_path)
def test_jit_load_no_path(self): def test_jit_load_no_path(self):
path = os.path.join( path = os.path.join(
......
...@@ -161,7 +161,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase): ...@@ -161,7 +161,7 @@ class TestLearningRateDecayDygraph(unittest.TestCase):
Step_scheduler.epoch() Step_scheduler.epoch()
Reducelr_scheduler.step(loss) Reducelr_scheduler.step(loss)
fluid.dygraph.save_dygraph(linear.state_dict(), "save_path") paddle.save(linear.state_dict(), "save_path.pdparams")
Exponential_scheduler_test = fluid.dygraph.ExponentialDecay( Exponential_scheduler_test = fluid.dygraph.ExponentialDecay(
learning_rate=0.1, learning_rate=0.1,
...@@ -174,8 +174,8 @@ class TestLearningRateDecayDygraph(unittest.TestCase): ...@@ -174,8 +174,8 @@ class TestLearningRateDecayDygraph(unittest.TestCase):
learning_rate=1.0, decay_rate=0.5, patience=5, cooldown=3 learning_rate=1.0, decay_rate=0.5, patience=5, cooldown=3
) )
fluid.dygraph.save_dygraph(adam1.state_dict(), "save_path") paddle.save(adam1.state_dict(), "save_path.pdopt")
_, opt_state = fluid.dygraph.load_dygraph("save_path") opt_state = paddle.load("save_path.pdopt")
adam_test = fluid.optimizer.Adam( adam_test = fluid.optimizer.Adam(
learning_rate=Exponential_scheduler_test, learning_rate=Exponential_scheduler_test,
parameter_list=linear.parameters(), parameter_list=linear.parameters(),
...@@ -187,8 +187,8 @@ class TestLearningRateDecayDygraph(unittest.TestCase): ...@@ -187,8 +187,8 @@ class TestLearningRateDecayDygraph(unittest.TestCase):
"epoch_num is different before and after set_dict", "epoch_num is different before and after set_dict",
) )
fluid.dygraph.save_dygraph(adam2.state_dict(), "save_path") paddle.save(adam2.state_dict(), "save_path.pdopt")
_, opt_state = fluid.dygraph.load_dygraph("save_path") opt_state = paddle.load("save_path.pdopt")
adam_test = fluid.optimizer.Adam( adam_test = fluid.optimizer.Adam(
learning_rate=Step_scheduler_test, learning_rate=Step_scheduler_test,
parameter_list=linear.parameters(), parameter_list=linear.parameters(),
...@@ -205,8 +205,8 @@ class TestLearningRateDecayDygraph(unittest.TestCase): ...@@ -205,8 +205,8 @@ class TestLearningRateDecayDygraph(unittest.TestCase):
"current learning rate is different before and after set_dict", "current learning rate is different before and after set_dict",
) )
fluid.dygraph.save_dygraph(adam3.state_dict(), "save_path") paddle.save(adam3.state_dict(), "save_path.pdopt")
_, opt_state = fluid.dygraph.load_dygraph("save_path") opt_state = paddle.load("save_path.pdopt")
adam_test = fluid.optimizer.Adam( adam_test = fluid.optimizer.Adam(
learning_rate=Reducelr_scheduler_test, learning_rate=Reducelr_scheduler_test,
parameter_list=linear.parameters(), parameter_list=linear.parameters(),
......
...@@ -148,9 +148,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase): ...@@ -148,9 +148,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase):
self.params_filename = None self.params_filename = None
orig_param_dict = self.train_and_save_model() orig_param_dict = self.train_and_save_model()
load_param_dict, _ = fluid.load_dygraph(self.save_dirname)
self.check_load_state_dict(orig_param_dict, load_param_dict)
new_load_param_dict = paddle.load(self.save_dirname) new_load_param_dict = paddle.load(self.save_dirname)
self.check_load_state_dict(orig_param_dict, new_load_param_dict) self.check_load_state_dict(orig_param_dict, new_load_param_dict)
...@@ -162,11 +159,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase): ...@@ -162,11 +159,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase):
self.params_filename = None self.params_filename = None
orig_param_dict = self.train_and_save_model() orig_param_dict = self.train_and_save_model()
load_param_dict, _ = fluid.load_dygraph(
self.save_dirname, model_filename=self.model_filename
)
self.check_load_state_dict(orig_param_dict, load_param_dict)
new_load_param_dict = paddle.load( new_load_param_dict = paddle.load(
self.save_dirname, model_filename=self.model_filename self.save_dirname, model_filename=self.model_filename
) )
...@@ -180,11 +172,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase): ...@@ -180,11 +172,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase):
self.params_filename = "static_mnist.params" self.params_filename = "static_mnist.params"
orig_param_dict = self.train_and_save_model() orig_param_dict = self.train_and_save_model()
load_param_dict, _ = fluid.load_dygraph(
self.save_dirname, params_filename=self.params_filename
)
self.check_load_state_dict(orig_param_dict, load_param_dict)
new_load_param_dict = paddle.load( new_load_param_dict = paddle.load(
self.save_dirname, params_filename=self.params_filename self.save_dirname, params_filename=self.params_filename
) )
...@@ -199,13 +186,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase): ...@@ -199,13 +186,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase):
self.params_filename = "static_mnist.params" self.params_filename = "static_mnist.params"
orig_param_dict = self.train_and_save_model() orig_param_dict = self.train_and_save_model()
load_param_dict, _ = fluid.load_dygraph(
self.save_dirname,
params_filename=self.params_filename,
model_filename=self.model_filename,
)
self.check_load_state_dict(orig_param_dict, load_param_dict)
new_load_param_dict = paddle.load( new_load_param_dict = paddle.load(
self.save_dirname, self.save_dirname,
params_filename=self.params_filename, params_filename=self.params_filename,
...@@ -220,9 +200,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase): ...@@ -220,9 +200,6 @@ class TestLoadStateDictFromSaveInferenceModel(unittest.TestCase):
self.params_filename = None self.params_filename = None
orig_param_dict = self.train_and_save_model(True) orig_param_dict = self.train_and_save_model(True)
load_param_dict, _ = fluid.load_dygraph(self.save_dirname)
self.check_load_state_dict(orig_param_dict, load_param_dict)
new_load_param_dict = paddle.load(self.save_dirname) new_load_param_dict = paddle.load(self.save_dirname)
self.check_load_state_dict(orig_param_dict, new_load_param_dict) self.check_load_state_dict(orig_param_dict, new_load_param_dict)
......
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import numpy as np import numpy as np
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.framework import _dygraph_guard from paddle.fluid.framework import _dygraph_guard
...@@ -94,7 +94,8 @@ def load_dygraph_vars_to_scope(model_path, scope, place): ...@@ -94,7 +94,8 @@ def load_dygraph_vars_to_scope(model_path, scope, place):
dst_t.set(np.array(src_t), place) dst_t.set(np.array(src_t), place)
dst_t.set_lod(src_t.lod()) dst_t.set_lod(src_t.lod())
param_dict, opti_dict = fluid.load_dygraph(model_path) param_dict = paddle.load(model_path + '.pdparams')
opti_dict = paddle.load(model_path + '.pdopt')
if param_dict: if param_dict:
load_dict_to_scope(scope, param_dict) load_dict_to_scope(scope, param_dict)
......
...@@ -155,7 +155,6 @@ def _load_state_dict_from_save_params(model_path): ...@@ -155,7 +155,6 @@ def _load_state_dict_from_save_params(model_path):
# - need [full filename] when loading # - need [full filename] when loading
# - paddle.save # - paddle.save
# - paddle.static.save # - paddle.static.save
# - paddle.fluid.save_dygraph
# - need [prefix] when loading [compatible for paddle 2.x] # - need [prefix] when loading [compatible for paddle 2.x]
# - paddle.jit.save # - paddle.jit.save
# - paddle.static.save_inference_model # - paddle.static.save_inference_model
...@@ -185,7 +184,6 @@ def _build_load_path_and_config(path, config): ...@@ -185,7 +184,6 @@ def _build_load_path_and_config(path, config):
opti_file_path = path + ".pdopt" opti_file_path = path + ".pdopt"
if os.path.exists(params_file_path) or os.path.exists(opti_file_path): if os.path.exists(params_file_path) or os.path.exists(opti_file_path):
error_msg += ( error_msg += (
" If you want to load the results saved by `fluid.save_dygraph`, "
"please specify the full file name, not just the file name prefix. For " "please specify the full file name, not just the file name prefix. For "
"example, it should be written as `paddle.load('model.pdparams')` instead of " "example, it should be written as `paddle.load('model.pdparams')` instead of "
"`paddle.load('model')`." "`paddle.load('model')`."
......
...@@ -899,11 +899,11 @@ class DynamicGraphAdapter: ...@@ -899,11 +899,11 @@ class DynamicGraphAdapter:
def save(self, path): def save(self, path):
params = self.model.network.state_dict() params = self.model.network.state_dict()
fluid.save_dygraph(params, path) paddle.save(params, path + '.pdparams')
if self.model._optimizer is not None: if self.model._optimizer is not None:
if self.model._optimizer.state_dict(): if self.model._optimizer.state_dict():
optim = self.model._optimizer.state_dict() optim = self.model._optimizer.state_dict()
fluid.save_dygraph(optim, path) paddle.save(optim, path + '.pdopt')
if hasattr(self.model, '_scaler') and self.model._scaler is not None: if hasattr(self.model, '_scaler') and self.model._scaler is not None:
if self.model._scaler.state_dict(): if self.model._scaler.state_dict():
scaler = self.model._scaler.state_dict() scaler = self.model._scaler.state_dict()
......
...@@ -24,7 +24,6 @@ import types ...@@ -24,7 +24,6 @@ import types
import numpy import numpy
from paddle.fluid.dygraph.container import Sequential
from paddle.fluid.dygraph.layers import Layer from paddle.fluid.dygraph.layers import Layer
from paddle.jit.dy2static.logging_utils import TranslatorLogger from paddle.jit.dy2static.logging_utils import TranslatorLogger
from paddle.jit.dy2static.utils import is_paddle_func, unwrap from paddle.jit.dy2static.utils import is_paddle_func, unwrap
...@@ -40,10 +39,6 @@ from .convert_operators import ( ...@@ -40,10 +39,6 @@ from .convert_operators import (
__all__ = [] __all__ = []
# The api(s) should be considered as plain function and convert
# them into static layer code.
PADDLE_NEED_CONVERT_APIS = [Sequential]
translator_logger = TranslatorLogger() translator_logger = TranslatorLogger()
CONVERSION_OPTIONS = "An attribute for a function that indicates conversion flags of the function in dynamic-to-static." CONVERSION_OPTIONS = "An attribute for a function that indicates conversion flags of the function in dynamic-to-static."
...@@ -125,6 +120,11 @@ def is_unsupported(func): ...@@ -125,6 +120,11 @@ def is_unsupported(func):
return True return True
# NOTE: should be placed before `is_paddle_func` # NOTE: should be placed before `is_paddle_func`
# The api(s) should be considered as plain function and convert
# them into static layer code.
from paddle.nn import Sequential
PADDLE_NEED_CONVERT_APIS = [Sequential]
if type(func) in PADDLE_NEED_CONVERT_APIS: if type(func) in PADDLE_NEED_CONVERT_APIS:
return False return False
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
from ..fluid.dygraph.layers import Layer # noqa: F401 from ..fluid.dygraph.layers import Layer # noqa: F401
from .layer.container import LayerList # noqa: F401 from .layer.container import LayerList # noqa: F401
from .layer.container import ParameterList # noqa: F401 from .layer.container import ParameterList # noqa: F401
from ..fluid.dygraph.container import Sequential # noqa: F401 from .layer.container import Sequential # noqa: F401
from .clip import ClipGradByGlobalNorm # noqa: F401 from .clip import ClipGradByGlobalNorm # noqa: F401
from .clip import ClipGradByNorm # noqa: F401 from .clip import ClipGradByNorm # noqa: F401
......
...@@ -532,3 +532,76 @@ class LayerList(Layer): ...@@ -532,3 +532,76 @@ class LayerList(Layer):
idx = str(offset + i) idx = str(offset + i)
self.add_sublayer(idx, sublayer) self.add_sublayer(idx, sublayer)
return self return self
class Sequential(Layer):
"""Sequential container.
Sub layers will be added to this container in the order of argument in the constructor.
The argument passed to the constructor can be iterable Layers or iterable name Layer pairs.
Parameters:
layers(Layer|list|tuple): Layer or list/tuple of iterable name Layer pair.
Examples:
.. code-block:: python
import paddle
data = paddle.uniform(shape=[30, 10], dtype='float32')
# create Sequential with iterable Layers
model1 = paddle.nn.Sequential(
paddle.nn.Linear(10, 1), paddle.nn.Linear(1, 2)
)
model1[0] # access the first layer
res1 = model1(data) # sequential execution
# create Sequential with name Layer pairs
model2 = paddle.nn.Sequential(
('l1', paddle.nn.Linear(10, 2)),
('l2', paddle.nn.Linear(2, 3))
)
model2['l1'] # access l1 layer
model2.add_sublayer('l3', paddle.nn.Linear(3, 3)) # add sublayer
res2 = model2(data) # sequential execution
"""
def __init__(self, *layers):
super().__init__()
if len(layers) > 0 and isinstance(layers[0], (list, tuple)):
for name, layer in layers:
self.add_sublayer(name, layer)
else:
for idx, layer in enumerate(layers):
self.add_sublayer(str(idx), layer)
def __getitem__(self, name):
if isinstance(name, slice):
return self.__class__(*(list(self._sub_layers.values())[name]))
elif isinstance(name, str):
return self._sub_layers[name]
else:
if name >= len(self._sub_layers):
raise IndexError('index {} is out of range'.format(name))
elif name < 0 and name >= -len(self._sub_layers):
name += len(self._sub_layers)
elif name < -len(self._sub_layers):
raise IndexError('index {} is out of range'.format(name))
return list(self._sub_layers.values())[name]
def __setitem__(self, name, layer):
assert isinstance(layer, Layer)
setattr(self, str(name), layer)
def __delitem__(self, name):
name = str(name)
assert name in self._sub_layers
del self._sub_layers[name]
def __len__(self):
return len(self._sub_layers)
def forward(self, input):
for layer in self._sub_layers.values():
input = layer(input)
return input
...@@ -228,7 +228,7 @@ class TestModel(unittest.TestCase): ...@@ -228,7 +228,7 @@ class TestModel(unittest.TestCase):
if not os.path.exists(cls.save_dir): if not os.path.exists(cls.save_dir):
os.makedirs(cls.save_dir) os.makedirs(cls.save_dir)
cls.weight_path = os.path.join(cls.save_dir, 'lenet') cls.weight_path = os.path.join(cls.save_dir, 'lenet')
fluid.dygraph.save_dygraph(dy_lenet.state_dict(), cls.weight_path) paddle.save(dy_lenet.state_dict(), cls.weight_path + '.pdparams')
fluid.disable_dygraph() fluid.disable_dygraph()
......
...@@ -1542,7 +1542,6 @@ FOURTH_HIGH_PARALLEL_JOB_NEW = [ ...@@ -1542,7 +1542,6 @@ FOURTH_HIGH_PARALLEL_JOB_NEW = [
'test_gru_unit_op', 'test_gru_unit_op',
'test_amp_check_finite_and_scale_op', 'test_amp_check_finite_and_scale_op',
'test_imperative_selected_rows_to_lod_tensor', 'test_imperative_selected_rows_to_lod_tensor',
'test_imperative_save_load',
'test_add_reader_dependency', 'test_add_reader_dependency',
'test_imperative_transformer_sorted_gradient', 'test_imperative_transformer_sorted_gradient',
'test_bicubic_interp_v2_op', 'test_bicubic_interp_v2_op',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册