checkpoint.py 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2018 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 __future__ import print_function

import os
import collections
from .. import core
L
lujun 已提交
20
from ..framework import Variable, default_main_program
21 22 23 24

__all__ = ['save_persistables', 'load_persistables']


L
lujun 已提交
25
def save_persistables(vardict, dirname, filename=None):
26 27 28 29 30 31 32 33 34 35 36
    """
    This function filters out all variables in layer.parameters from the
    give `layer` and then trys to load these variables from the folder
    `dirname` or the file `filename`.

    Use the `dirname` to specify the folder where persistable variables were
    saved. If variables were saved in separate files, set `filename` None;
    if all variables were saved in a single file, use `filename` to specify
    the file name.

    Args:
L
lujun 已提交
37
        vardict(dict of Parameters): The parameters will
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
                                    be saved. If it is None, nothing
                                    will be deal.
        dirname(str): The directory path.
        filename(str|None): The file which saved all variables. If variables were
                            saved in differnet files, set it to None.
                            Default: None

    Returns:

    Examples:
        .. code-block:: python
            ptb_model = PtbModel(
                hidden_size=hidden_size,
                vocab_size=vocab_size,
                num_layers=num_layers,
                num_steps=num_steps,
                init_scale=init_scale)

            x_data = np.arange(12).reshape(4, 3).astype('int64')
            y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
            x_data = x_data.reshape((-1, num_steps, 1))
            y_data = y_data.reshape((-1, 1))
            init_hidden_data = np.zeros(
                (num_layers, batch_size, hidden_size), dtype='float32')
            init_cell_data = np.zeros(
                (num_layers, batch_size, hidden_size), dtype='float32')
            x = to_variable(x_data)
            y = to_variable(y_data)
            init_hidden = to_variable(init_hidden_data)
            init_cell = to_variable(init_cell_data)
            dy_loss, last_hidden, last_cell = ptb_model(x, y, init_hidden,
                                                        init_cell)
            param_path = "./my_paddle_model"
L
lujun 已提交
71
            fluid.dygraph.save_persistables(ptb_model.state_dict(), dirname=param_path,
72 73
                                       layer=ptb_model)
    """
L
lujun 已提交
74 75
    if isinstance(vardict, collections.OrderedDict):
        _save_var_to_file(vardict, dirname, filename)
76 77


L
lujun 已提交
78
def load_persistables(vardict, dirname, filename=None):
79 80 81 82 83 84 85 86 87 88
    """
    This function trys to load persistable variables from the folder
    `dirname` or the file `filename`.

    Use the `dirname` to specify the folder where persistable variables were
    saved. If variables were saved in separate files, set `filename` None;
    if all variables were saved in a single file, use `filename` to specify
    the file name.

    Args:
L
lujun 已提交
89
        vardict(dict of Parameters): The parameters will be loaded.
90 91 92 93 94 95 96 97 98 99
        dirname(str): The directory path.
        filename(str|None): The file which saved all variables, this file path should be end with '.npz'. If variables were
                            saved in differnet files, set it to None.
                            Default: None

    Returns:
        dict: The parameter-dict resumed from file

    Examples:
        .. code-block:: python
L
lujun 已提交
100
            my_layer = layer(fluid.dygraph.Layer)
101 102
            param_path = "./my_paddle_model"

L
lujun 已提交
103
            param_dict = fluid.dygraph.load_persistables(my_layer.parameters(), param_path)
104 105 106
            param_1 = param_dict['PtbModel_0.w_1']

            or:
L
lujun 已提交
107
            my_layer = layer(fluid.dygraph.Layer)
108 109
            param_path = "./my_paddle_model"
            filename = "model.file"
L
lujun 已提交
110
            param_dict = fluid.dygraph.load_persistables(my_layer.state_dict(), param_path,
111 112 113 114
                                                                       filename=filename)
            param_1 = param_dict['PtbModel_0.w_1']

        """
L
lujun 已提交
115 116
    if isinstance(vardict, collections.OrderedDict):
        return _load_var_from_file(vardict, dirname, filename)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

    return {}


def _save_var_to_file(stat_dict, file_dir, file_name):
    save_block = default_main_program().global_block()
    save_var_map = {}
    for each_var in stat_dict.items():
        save_var_map[each_var.name] = each_var
        if file_name is None:
            save_block.append_op(
                type='save',
                inputs={'X': [each_var]},
                outputs={},
                attrs={'file_path': os.path.join(file_dir, each_var.name)})

    if file_name is not None:
        save_var_list = []
        for name in sorted(save_var_map.keys()):
            save_var_list.append(save_var_map[name])

        save_block.append_op(
            type='save_combine',
            inputs={'X': save_var_list},
            outputs={},
            attrs={'file_path': os.path.join(file_dir, file_name)})


def _load_var_from_file(stat_dict, file_dir, file_name):
    load_block = default_main_program().global_block()
    load_var_map = {}

    for each_var in stat_dict.items():
        assert isinstance(each_var, Variable)
        if each_var.type == core.VarDesc.VarType.RAW:
            continue
        new_var = _clone_var_in_block_(load_block, each_var)
        if file_name is None:
            load_block.append_op(
                type='load',
                inputs={},
                outputs={'Out': [new_var]},
                attrs={'file_path': os.path.join(file_dir, each_var.name)})

        load_var_map[new_var.name] = new_var

    if file_name is not None:
        load_var_list = []
        for name in sorted(load_var_map.keys()):
            load_var_list.append(load_var_map[name])

        load_block.append_op(
            type='load_combine',
            inputs={},
            outputs={"Out": load_var_list},
            attrs={'file_path': os.path.join(file_dir, file_name)})
        for res_var in load_var_list:
            load_var_map[res_var.name] = res_var

    return load_var_map


def _clone_var_in_block_(block, var):
    assert isinstance(var, Variable)
    return block.create_var(
        name=var.name,
        shape=var.shape,
        dtype=var.dtype,
        type=var.type,
        lod_level=var.lod_level,
        persistable=True)