checkpoint.py 11.7 KB
Newer Older
1
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# 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
17
import functools
18 19 20 21 22 23 24 25 26 27 28
from ..framework import (
    Variable,
    default_main_program,
    _non_static_mode,
    dygraph_only,
    Parameter,
    ParamBase,
    _varbase_creator,
    _dygraph_tracer,
    EagerParamBase,
)
29 30 31
import pickle
from . import learning_rate_scheduler
import warnings
H
hong 已提交
32
from .. import core
33
from .base import guard
34
from paddle.jit.api import _SaveLoadConfig
35
from paddle.jit.translated_layer import (
36 37 38
    _construct_program_holders,
    _construct_params_and_buffers,
)
39

H
hong 已提交
40 41 42 43
__all__ = [
    'save_dygraph',
    'load_dygraph',
]
44 45


46 47 48 49 50 51 52 53
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."
54 55
                % (key)
            )
56

57 58 59 60 61
    # 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)
62

63
    return inner_config
64 65


H
hong 已提交
66 67 68
@dygraph_only
def save_dygraph(state_dict, model_path):
    '''
69 70
    :api_attr: imperative

H
hong 已提交
71
    Save Layer's state_dict to disk. This will generate a file with suffix ".pdparams"
72

H
hong 已提交
73
    The state_dict is get from Layers.state_dict function
74

75
    Args:
H
hong 已提交
76 77
        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
78 79

    Returns:
L
lujun 已提交
80
        None
81 82

    Examples:
H
hong 已提交
83 84 85
        .. code-block:: python

            import paddle.fluid as fluid
86
            import paddle
H
hong 已提交
87 88

            with fluid.dygraph.guard():
89
                emb = paddle.nn.Embedding(10, 10)
H
hong 已提交
90 91 92 93

                state_dict = emb.state_dict()
                fluid.save_dygraph( state_dict, "paddle_dy")

94 95
                adam = fluid.optimizer.Adam( learning_rate = fluid.layers.noam_decay( 100, 10000),
                                             parameter_list = emb.parameters() )
H
hong 已提交
96 97 98 99 100 101 102

                state_dict = adam.state_dict()
                fluid.save_dygraph( state_dict, "paddle_dy")

    '''

    base_name = os.path.basename(model_path)
103 104 105
    assert (
        base_name != ""
    ), "The input model_path MUST be format of dirname/filename [dirname\\filename in Windows system], but received filename is empty string."
H
hong 已提交
106 107 108 109

    suffix = ".pdparams"
    assert len(state_dict) > 0, "state_dict is empty, no need to save"

110
    param_num = 0
H
hong 已提交
111
    for k, v in state_dict.items():
112
        if isinstance(v, (ParamBase, EagerParamBase)):
113 114 115 116
            param_num += 1

    if param_num == 0:
        suffix = ".pdopt"
H
hong 已提交
117

H
hong 已提交
118 119 120
    model_dict = {}
    name_table = {}
    for k, v in state_dict.items():
121
        if isinstance(v, (Variable, core.VarBase, core.eager.Tensor)):
H
hong 已提交
122
            model_dict[k] = v.numpy()
123
            name_table[k] = v.name
H
hong 已提交
124 125 126 127
        else:
            model_dict[k] = v
    model_dict["StructuredToParameterName@@"] = name_table

128 129 130 131 132 133
    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:
134
        pickle.dump(model_dict, f, protocol=2)
H
hong 已提交
135 136


137
# NOTE(chenweihang): load_dygraph will deprecated in future, we don't
138
# support new loading features for it
139 140
# TODO(qingqing01): remove dygraph_only to support loading static model.
# maybe need to unify the loading interface after 2.0 API is ready.
141
# @dygraph_only
142
def load_dygraph(model_path, **configs):
H
hong 已提交
143
    '''
144
    :api_attr: imperative
145

146 147 148
    Load parameter state dict from disk.

    .. note::
149 150 151
        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`
152
        when using `Layer.set_state_dict` later.
H
hong 已提交
153 154

    Args:
155 156 157
        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
158 159
            recommend using these configurations, if not necessary, DO NOT use them. Default None.
            The following options are currently supported:
160 161 162
            (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``
163
            save format. No default file name, save variables separately by default.
H
hong 已提交
164 165 166

    Returns:
        state_dict(dict) : the dict store the state_dict
L
lujun 已提交
167

H
hong 已提交
168
    Examples:
169
        .. code-block:: python
L
lujun 已提交
170

171
            import paddle
172 173
            import paddle.fluid as fluid

174
            paddle.disable_static()
H
hong 已提交
175

176
            emb = paddle.nn.Embedding(10, 10)
H
hong 已提交
177

178
            state_dict = emb.state_dict()
179
            fluid.save_dygraph(state_dict, "paddle_dy")
H
hong 已提交
180

181
            scheduler = paddle.optimizer.lr.NoamDecay(
182 183 184 185 186
                d_model=0.01, warmup_steps=100, verbose=True)
            adam = paddle.optimizer.Adam(
                learning_rate=scheduler,
                parameters=emb.parameters())
            state_dict = adam.state_dict()
187
            fluid.save_dygraph(state_dict, "paddle_dy")
H
hong 已提交
188

189
            para_state_dict, opti_state_dict = fluid.load_dygraph("paddle_dy")
190 191
    '''
    # deal with argument `model_path`
192 193 194 195 196 197
    model_prefix = model_path
    if model_prefix.endswith(".pdparams"):
        model_prefix = model_prefix[:-9]
    elif model_prefix.endswith(".pdopt"):
        model_prefix = model_prefix[:-6]

198
    para_dict = None
H
hong 已提交
199
    opti_dict = None
200
    params_file_path = model_prefix + ".pdparams"
201
    opti_file_path = model_prefix + ".pdopt"
202

203
    # deal with argument `config`
204
    config = _parse_load_config(configs)
205

206
    if os.path.exists(params_file_path) or os.path.exists(opti_file_path):
207
        # Load state dict by `save_dygraph` save format
M
MRXLT 已提交
208
        para_dict = {}
209 210
        if os.path.exists(params_file_path):
            with open(params_file_path, 'rb') as f:
T
tianshuo78520a 已提交
211
                para_dict = pickle.load(f, encoding='latin1')
212

213 214 215 216
        if (
            not config.keep_name_table
            and "StructuredToParameterName@@" in para_dict
        ):
217 218 219 220
            del para_dict["StructuredToParameterName@@"]

        if os.path.exists(opti_file_path):
            with open(opti_file_path, 'rb') as f:
T
tianshuo78520a 已提交
221
                opti_dict = pickle.load(f, encoding='latin1')
222 223 224
    else:
        # check model path
        if not os.path.isdir(model_prefix):
225 226 227
            raise ValueError(
                "Model saved directory '%s' is not exists." % model_prefix
            )
228 229 230 231 232 233 234 235 236 237 238

        # 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 ]
239 240 241
            # 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
242
            # the user to configure the `use_structured_name` argument when `set_state_dict`
243
            # NOTE(chenweihang): `jit.save` doesn't save optimizer state
244 245

            # 1. load program desc & construct _ProgramHolder
246 247 248
            programs = _construct_program_holders(
                model_path, config.model_filename
            )
249 250 251 252 253 254 255

            # 2. load layer parameters & buffers
            with guard():
                persistable_var_dict = _construct_params_and_buffers(
                    model_prefix,
                    programs,
                    config.params_filename,
256 257
                    append_suffix=False,
                )
258 259 260 261 262 263

                # 3. construct state_dict
                para_dict = dict()
                for var_name in persistable_var_dict:
                    para_dict[var_name] = persistable_var_dict[var_name].numpy()

264 265 266
                # 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)
267 268 269 270 271 272
                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(
273 274 275 276 277 278
                            'structured_name', None
                        )
                        assert structured_name is not None, (
                            "Cannot find saved variable (%s)'s structured name in saved model."
                            % var_name
                        )
279
                        structured_para_dict[structured_name] = para_dict[
280 281
                            var_name
                        ]
282 283 284
                    para_dict = structured_para_dict
        else:
            # load state dict by `io.save_params/persistables` save format
285
            # TODO(chenweihang): [ Now only supports loading parameters separately ]
286
            # If users save all parameters as one file, the [ variable.name -> variable ]
287
            # mapping info will lost, so users need to give variable list, but users build
288
            # variable list in dygraph mode is difficult, we recommend users to use
289
            # paddle.static.load_program_state in this case
290

291
            # Try to load all the files in the directory in VarBase format,
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
            # 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},
312 313
                        attrs={'file_path': os.path.join(model_path, name)},
                    )
314 315 316 317 318 319
                    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()
H
hong 已提交
320 321

    return para_dict, opti_dict