io.py 14.9 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
import os

17 18
from paddle.fluid.evaluator import Evaluator
from paddle.fluid.framework import Program, Parameter, default_main_program, Variable
K
fix bug  
Kexin Zhao 已提交
19
from . import core
20 21

__all__ = [
22 23 24 25 26 27 28 29 30
    'save_vars',
    'save_params',
    'save_persistables',
    'load_vars',
    'load_params',
    'load_persistables',
    'save_inference_model',
    'load_inference_model',
    'get_inference_program',
31 32 33 34
]


def is_parameter(var):
K
Kavya Srinet 已提交
35
    """Check whether the variable is a Parameter.
36 37 38 39 40 41 42

    This function checks whether the input variable is a Parameter.

    Args:
        var : The input variable.

    Returns:
K
Kavya Srinet 已提交
43
        boolean result whether the variable is a Parameter.
44
    """
45 46 47 48
    return isinstance(var, Parameter)


def is_persistable(var):
49 50 51
    if var.desc.type() == core.VarDesc.VarType.FEED_MINIBATCH or \
       var.desc.type() == core.VarDesc.VarType.FETCH_LIST:
        return False
52 53 54 55 56 57 58 59
    return var.persistable


def _clone_var_in_block_(block, var):
    assert isinstance(var, Variable)
    return block.create_var(
        name=var.name,
        shape=var.shape,
F
fengjiayi 已提交
60
        dtype=var.dtype,
61 62 63 64 65
        type=var.type,
        lod_level=var.lod_level,
        persistable=True)


66 67 68 69 70
def save_vars(executor,
              dirname,
              main_program=None,
              vars=None,
              predicate=None,
71
              filename=None):
72 73
    """
    Save variables to directory by executor.
74

75 76
    :param executor: executor that save variable
    :param dirname: directory path
X
xuwei06 已提交
77
    :param main_program: program. If vars is None, then filter all variables in this
78
    program which fit `predicate`. Default default_main_program.
79
    :param predicate: The Predicate describes a callable that returns a variable
80 81
    as a bool. If it returns true, the corresponding input variable will be saved.
    :param vars: variables need to be saved. If vars is specified, program & predicate
82
    will be ignored
83 84
    :param filename: The name of a single file that all vars are saved to.
        If it is None, save variables to separate files.
85

86 87 88
    :return: None
    """
    if vars is None:
89
        if main_program is None:
Y
Yu Yang 已提交
90
            main_program = default_main_program()
91
        if not isinstance(main_program, Program):
92 93 94 95 96
            raise TypeError("program should be as Program type or None")

        save_vars(
            executor,
            dirname=dirname,
97
            vars=filter(predicate, main_program.list_vars()),
98
            filename=filename)
99 100 101
    else:
        save_program = Program()
        save_block = save_program.global_block()
102 103

        save_var_map = {}
104 105
        for each_var in vars:
            new_var = _clone_var_in_block_(save_block, each_var)
106
            if filename is None:
107 108 109 110 111 112 113 114
                save_block.append_op(
                    type='save',
                    inputs={'X': [new_var]},
                    outputs={},
                    attrs={'file_path': os.path.join(dirname, new_var.name)})
            else:
                save_var_map[new_var.name] = new_var

115
        if filename is not None:
116 117 118 119
            save_var_list = []
            for name in sorted(save_var_map.keys()):
                save_var_list.append(save_var_map[name])

120
            save_block.append_op(
121 122
                type='save_combine',
                inputs={'X': save_var_list},
123
                outputs={},
124
                attrs={'file_path': os.path.join(dirname, filename)})
125

126 127 128
        executor.run(save_program)


129
def save_params(executor, dirname, main_program=None, filename=None):
130 131 132 133 134 135
    """
    Save all parameters to directory with executor.
    """
    save_vars(
        executor,
        dirname=dirname,
136
        main_program=main_program,
137
        vars=None,
138
        predicate=is_parameter,
139
        filename=filename)
140 141


142
def save_persistables(executor, dirname, main_program=None, filename=None):
143 144 145 146 147 148
    """
    Save all persistables to directory with executor.
    """
    save_vars(
        executor,
        dirname=dirname,
149
        main_program=main_program,
150
        vars=None,
151
        predicate=is_persistable,
152
        filename=filename)
153 154


155 156 157 158 159
def load_vars(executor,
              dirname,
              main_program=None,
              vars=None,
              predicate=None,
160
              filename=None):
161 162
    """
    Load variables from directory by executor.
163

164
    :param executor: executor that load variable
165
    :param dirname: directory path
X
xuwei06 已提交
166
    :param main_program: program. If vars is None, then filter all variables in this
Y
Yu Yang 已提交
167
    program which fit `predicate`. Default default_main_program().
168
    :param predicate: The Predicate describes a callable that returns a variable
169 170
    as a bool. If it returns true, the corresponding input variable will be loaded.
    :param vars: variables need to be loaded. If vars is specified, program &
171
    predicate will be ignored
172 173
    :param filename: The name of the single file that all vars are loaded from.
        If it is None, load variables from separate files.
174

175 176 177
    :return: None
    """
    if vars is None:
178
        if main_program is None:
Y
Yu Yang 已提交
179
            main_program = default_main_program()
180
        if not isinstance(main_program, Program):
181 182 183 184 185
            raise TypeError("program's type should be Program")

        load_vars(
            executor,
            dirname=dirname,
186
            vars=filter(predicate, main_program.list_vars()),
187
            filename=filename)
188 189 190
    else:
        load_prog = Program()
        load_block = load_prog.global_block()
191 192

        load_var_map = {}
193 194 195
        for each_var in vars:
            assert isinstance(each_var, Variable)
            new_var = _clone_var_in_block_(load_block, each_var)
196
            if filename is None:
197 198 199 200 201 202 203 204
                load_block.append_op(
                    type='load',
                    inputs={},
                    outputs={'Out': [new_var]},
                    attrs={'file_path': os.path.join(dirname, new_var.name)})
            else:
                load_var_map[new_var.name] = new_var

205
        if filename is not None:
206 207 208 209
            load_var_list = []
            for name in sorted(load_var_map.keys()):
                load_var_list.append(load_var_map[name])

210
            load_block.append_op(
211
                type='load_combine',
212
                inputs={},
213
                outputs={"Out": load_var_list},
214
                attrs={'file_path': os.path.join(dirname, filename)})
215

216 217 218
        executor.run(load_prog)


219
def load_params(executor, dirname, main_program=None, filename=None):
220 221 222 223
    """
    load all parameters from directory by executor.
    """
    load_vars(
224 225 226
        executor,
        dirname=dirname,
        main_program=main_program,
227
        predicate=is_parameter,
228
        filename=filename)
229 230


231
def load_persistables(executor, dirname, main_program=None, filename=None):
232 233 234 235
    """
    load all persistables from directory by executor.
    """
    load_vars(
236 237 238
        executor,
        dirname=dirname,
        main_program=main_program,
239
        predicate=is_persistable,
240
        filename=filename)
241 242


243 244
def get_inference_program(target_vars, main_program=None):
    if main_program is None:
Y
Yu Yang 已提交
245
        main_program = default_main_program()
246 247
    if not isinstance(target_vars, list):
        target_vars = [target_vars]
W
wanghaoshuang 已提交
248 249 250
    vars = []
    for var in target_vars:
        if isinstance(var, Evaluator):
W
wanghaoshuang 已提交
251 252
            vars.extend(var.states)
            vars.extend(var.metrics)
W
wanghaoshuang 已提交
253 254 255
        else:
            vars.append(var)
    pruned_program = main_program.prune(targets=vars)
256 257 258 259
    inference_program = pruned_program.inference_optimize()
    return inference_program


260 261 262
def prepend_feed_ops(inference_program,
                     feed_target_names,
                     feed_holder_name='feed'):
K
Kexin Zhao 已提交
263 264
    global_block = inference_program.global_block()
    feed_var = global_block.create_var(
265 266 267
        name=feed_holder_name,
        type=core.VarDesc.VarType.FEED_MINIBATCH,
        persistable=True)
K
Kexin Zhao 已提交
268

269
    for i, name in enumerate(feed_target_names):
K
fix bug  
Kexin Zhao 已提交
270
        out = global_block.var(name)
K
Kexin Zhao 已提交
271 272 273
        global_block.prepend_op(
            type='feed',
            inputs={'X': [feed_var]},
K
fix bug  
Kexin Zhao 已提交
274
            outputs={'Out': [out]},
K
Kexin Zhao 已提交
275 276 277
            attrs={'col': i})


278 279 280
def append_fetch_ops(inference_program,
                     fetch_target_names,
                     fetch_holder_name='fetch'):
K
Kexin Zhao 已提交
281 282
    global_block = inference_program.global_block()
    fetch_var = global_block.create_var(
283 284 285
        name=fetch_holder_name,
        type=core.VarDesc.VarType.FETCH_LIST,
        persistable=True)
K
Kexin Zhao 已提交
286

287
    for i, name in enumerate(fetch_target_names):
K
Kexin Zhao 已提交
288 289 290 291 292 293 294
        global_block.append_op(
            type='fetch',
            inputs={'X': [name]},
            outputs={'Out': [fetch_var]},
            attrs={'col': i})


295 296 297 298
def save_inference_model(dirname,
                         feeded_var_names,
                         target_vars,
                         executor,
299
                         main_program=None,
300 301
                         model_filename=None,
                         params_filename=None):
302
    """
X
xuwei06 已提交
303
    Build a model especially for inference,
304 305 306 307 308 309
    and save it to directory by the executor.

    :param dirname: directory path
    :param feeded_var_names: Names of variables that need to be feeded data during inference
    :param target_vars: Variables from which we can get inference results.
    :param executor: executor that save inference model
X
xuwei06 已提交
310
    :param main_program: original program, which will be pruned to build the inference model.
Y
Yu Yang 已提交
311
            Default default_main_program().
312 313 314 315 316
    :param model_filename: The name of file to save inference program.
        If not specified, default filename `__model__` will be used.
    :param params_filename: The name of file to save parameters.
        It is used for the case that all parameters are saved in a single binary file.
        If not specified, parameters are considered saved in separate files.
317 318 319

    :return: None
    """
F
fengjiayi 已提交
320 321 322 323 324 325 326 327
    if isinstance(feeded_var_names, basestring):
        feeded_var_names = [feeded_var_names]
    else:
        if not (bool(feeded_var_names) and all(
                isinstance(name, basestring) for name in feeded_var_names)):
            raise ValueError("'feed_var_names' should be a list of str.")

    if isinstance(target_vars, Variable):
F
fengjiayi 已提交
328
        target_vars = [target_vars]
F
fengjiayi 已提交
329 330 331 332 333
    else:
        if not (bool(target_vars) and all(
                isinstance(var, Variable) for var in target_vars)):
            raise ValueError("'target_vars' should be a list of Variable.")

334
    if main_program is None:
Y
Yu Yang 已提交
335
        main_program = default_main_program()
336 337 338 339

    if not os.path.isdir(dirname):
        os.makedirs(dirname)

340 341
    pruned_program = main_program.prune(targets=target_vars)
    inference_program = pruned_program.inference_optimize()
342 343
    fetch_var_names = [v.name for v in target_vars]

K
Kexin Zhao 已提交
344 345
    prepend_feed_ops(inference_program, feeded_var_names)
    append_fetch_ops(inference_program, fetch_var_names)
346

347 348
    if model_filename is not None:
        model_filename = os.path.basename(model_filename)
349
    else:
350 351
        model_filename = "__model__"
    model_filename = os.path.join(dirname, model_filename)
352

353 354 355 356
    if params_filename is not None:
        params_filename = os.path.basename(params_filename)

    with open(model_filename, "wb") as f:
357
        f.write(inference_program.desc.serialize_to_string())
358

359
    save_persistables(executor, dirname, inference_program, params_filename)
360 361


362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
def get_feed_targets_names(program):
    feed_targets_names = []
    global_block = program.global_block()
    for op in global_block.ops:
        if op.desc.type() == 'feed':
            feed_targets_names.insert(0, op.desc.output('Out')[0])
    return feed_targets_names


def get_fetch_targets_names(program):
    fetch_targets_names = []
    global_block = program.global_block()
    for op in global_block.ops:
        if op.desc.type() == 'fetch':
            fetch_targets_names.append(op.desc.input('X')[0])
    return fetch_targets_names


380 381 382 383
def load_inference_model(dirname,
                         executor,
                         model_filename=None,
                         params_filename=None):
384 385 386 387 388
    """
    Load inference model from a directory

    :param dirname: directory path
    :param executor: executor that load inference model
389 390 391 392 393 394
    :param model_filename: The name of file to load inference program.
        If not specified, default filename `__model__` will be used.
    :param params_filename: The name of file to load parameters.
        It is used for the case that all parameters are saved in a single binary file.
        If not specified, parameters are considered saved in separate files.

395
    :return: [program, feed_target_names, fetch_targets]
396
             program: program especially for inference.
397 398
             feed_target_names: Names of variables that need to feed data
             fetch_targets: Variables from which we can get inference results.
399 400 401 402
    """
    if not os.path.isdir(dirname):
        raise ValueError("There is no directory named '%s'", dirname)

403 404
    if model_filename is not None:
        model_filename = os.path.basename(model_filename)
405
    else:
406 407 408 409 410
        model_filename = "__model__"
    model_filename = os.path.join(dirname, model_filename)

    if params_filename is not None:
        params_filename = os.path.basename(params_filename)
411

412
    with open(model_filename, "rb") as f:
413 414
        program_desc_str = f.read()

415
    program = Program.parse_from_string(program_desc_str)
416
    load_persistables(executor, dirname, program, params_filename)
417

418 419 420 421 422 423 424
    feed_target_names = get_feed_targets_names(program)
    fetch_target_names = get_fetch_targets_names(program)
    fetch_targets = [
        program.global_block().var(name) for name in fetch_target_names
    ]

    return [program, feed_target_names, fetch_targets]
X
xuwei06 已提交
425 426 427 428 429 430 431 432


def get_parameter_value(para, executor):
    """
    Get the LoDTensor for the parameter

    :param executor: executor for retrieving the value
    :param para: the given parameter
433

X
xuwei06 已提交
434 435
    :return: the LoDTensor for the parameter
    """
X
xuwei06 已提交
436 437
    assert is_parameter(para)

X
xuwei06 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450
    get_program = Program()
    block = get_program.global_block()
    new_var = _clone_var_in_block_(block, para)
    return executor.run(get_program, feed={}, fetch_list=[new_var])[0]


def get_parameter_value_by_name(name, executor, program=None):
    """
    Get the LoDTensor for paramter with the given name

    :param executor: executor for retrieving the value
    :param name: the name of the parameter
    :param program: the program where the variable is found
Y
Yu Yang 已提交
451
            Default default_main_program().
452

X
xuwei06 已提交
453 454 455
    :return: the LoDTensor for the variable
    """
    if program is None:
Y
Yu Yang 已提交
456
        program = default_main_program()
X
xuwei06 已提交
457 458
    var = program.global_block().var(name)
    return get_parameter_value(var, executor)