lookup_table_utils.py 18.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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.
T
tangwei12 已提交
14
"""lookup_table_utils.py will move to fluid/incubate/fleet/utils/lookup_table.py"""
15 16 17 18 19 20 21

from __future__ import print_function

import os
import time
import logging

22
import paddle
23 24 25
from paddle.fluid import core
from paddle.fluid import io
from paddle.fluid import Program
26
from paddle.fluid.log_helper import get_logger
27 28

__all__ = [
T
tangwei12 已提交
29
    "load_persistables_for_increment", "load_persistables_for_inference",
30 31 32
    "convert_dist_to_sparse_program"
]

33 34 35 36
_logger = get_logger(
    'lookup_table_utils',
    logging.INFO,
    fmt='%(asctime)s-%(levelname)s: %(message)s')
37 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84

model_filename = "__model__"
lookup_table_dir = "__lookup_table__"


def __insert_lookup_sparse_table_op(main_program, idx, ids, w, out):
    main_program.global_block()._insert_op(
        index=idx,
        type="lookup_sparse_table",
        inputs={"Ids": [ids],
                "W": [w]},
        outputs={"Out": [out]},
        attrs={
            "is_distributed": False,
            "is_sparse": True,
            "grad_inplace": False
        })


def __get_prefetch_op_tuples(main_program):
    # current lookup tables op is split_ids->prefetch->merge_ids
    prefetch_op_tuples = None
    op_types = [op.type for op in main_program.global_block().ops]

    for i in range(len(op_types)):
        if op_types[i] == "prefetch":
            if op_types[i - 1] == "split_ids" and op_types[i +
                                                           1] == "merge_ids":
                split_ids_op_id = i - 1
                split_ids_inputs = main_program.global_block().ops[i - 1].input(
                    "Ids")
                prefetch_op_inputs = main_program.global_block().ops[i].input(
                    "X")
                prefetch_op_outputs = main_program.global_block().ops[i].output(
                    "Out")
                merge_ids_outputs = main_program.global_block().ops[
                    i + 1].output("Out")

                need_delete_vars = []
                need_delete_vars.extend(prefetch_op_inputs)
                need_delete_vars.extend(prefetch_op_outputs)

                prefetch_op_tuples = (split_ids_op_id, split_ids_inputs,
                                      merge_ids_outputs, need_delete_vars)
                break
    return prefetch_op_tuples


T
tangwei12 已提交
85 86 87 88 89 90
def convert_dist_to_sparse_program(program):
    """
    WARNING: this function will only be used for distributed training with distributed lookup table.
    when we train model with distributed lookup table but want to do the local inference, we can use
    this function to convert the train program with distributed lookup table to sparse lookup table.

91 92 93
    Args:
        program(Program): the program must be the trainer program, which will be get by the distribute transpiler.
    Returns:
T
tangwei12 已提交
94 95 96
        program: The `program` is a Program, it's the program replace distributed lookup table to sparse lookup table.
    """
    if not program._distributed_lookup_table:
97 98 99 100 101
        _logger.warn(
            "There are no distributed lookup tables need to be converted")
        return

    # create table param and grad var in pserver program
T
tangwei12 已提交
102 103 104 105
    origin_emb_var = "{}.origin".format(program._distributed_lookup_table)
    emb_var = program._distributed_lookup_table
    program.global_block()._rename_var(emb_var, origin_emb_var)
    origin_param_var = program.global_block().vars[origin_emb_var]
106

T
tangwei12 已提交
107
    param_var = program.global_block().create_var(
108 109 110 111 112 113 114
        name=emb_var,
        shape=origin_param_var.shape,
        dtype=origin_param_var.dtype,
        type=core.VarDesc.VarType.SELECTED_ROWS,
        persistable=True)
    # parameter must be selected rows
    param_var.desc.set_type(core.VarDesc.VarType.SELECTED_ROWS)
T
tangwei12 已提交
115
    program._sync_with_cpp()
116

T
tangwei12 已提交
117
    prefetch_op_tuples = __get_prefetch_op_tuples(program)
118 119 120 121

    split_ids_id = prefetch_op_tuples[0]

    for idx in range(split_ids_id + 2, split_ids_id - 1, -1):
T
tangwei12 已提交
122 123
        program.global_block()._remove_op(idx)
    program.desc.flush()
124 125 126 127 128

    in_out_pairs = zip(prefetch_op_tuples[1], prefetch_op_tuples[2])

    for in_out_pair in in_out_pairs:
        idx = split_ids_id
T
tangwei12 已提交
129 130 131 132 133
        ids = program.global_block().vars[in_out_pair[0]]
        out = program.global_block().vars[in_out_pair[1]]
        __insert_lookup_sparse_table_op(program, idx, ids, param_var, out)
        program.desc.flush()
    return program
134 135


T
tangwei12 已提交
136 137 138 139
def load_persistables_for_increment(dirname, executor, program,
                                    lookup_table_var, lookup_table_var_path):
    """
    WARNING: this function will only be used for distributed training with distributed lookup table.
T
tianshuo78520a 已提交
140
    for increment training, the pserver will not only load dense variables,
141 142 143 144 145 146 147 148 149 150 151 152 153
    but also load the suitable lookup table var. Because of sliced lookup table
    var with HASH, we must load the correct sliced var.

    Args:
        dirname(str): The directory path
        executor(Executor): The executor to run for loading inference model.
        program(Program): The parameter server program, which will run on Pserver.
        lookup_table_var: the distributed lookup tables var name.
        lookup_table_var_path: the the distributed lookup tables var location.

    Returns:
        None
    """
T
tangwei12 已提交
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    def _load_persistable_vars(executor, dirname, need_load_vars):
        load_prog = Program()
        load_block = load_prog.global_block()
        need_delete_vars = []

        for param in need_load_vars:
            origin_var = param.origin
            slice_var = param.slice
            is_slice = param.is_slice
            offset = param.offset

            if is_slice:
                origin = load_block.create_var(
                    name="{}.load".format(origin_var.name),
                    type=origin_var.type,
                    shape=origin_var.shape,
                    dtype=origin_var.dtype,
                    persistable=True)

                load_block.append_op(
                    type='load',
                    inputs={},
                    outputs={'Out': [origin]},
                    attrs={
                        'file_path': os.path.join(dirname, origin_var.name)
                    })

                slice = load_block.create_var(
                    name=slice_var.name,
                    type=slice_var.type,
                    shape=slice_var.shape,
                    dtype=slice_var.dtype,
                    persistable=True)

                dim1_flatten = reduce(lambda x, y: x * y, slice.shape[1:])
                start = int(offset / dim1_flatten)
                end = int(offset / dim1_flatten + slice.shape[0])

                load_block.append_op(
                    type="slice",
                    inputs={'Input': origin},
                    outputs={'Out': slice},
                    attrs={'axes': [0],
                           'starts': [start],
                           'ends': [end]})

                need_delete_vars.append(origin)
            else:
                origin = load_block.create_var(
                    name="{}".format(origin_var.name),
                    type=origin_var.type,
                    shape=origin_var.shape,
                    dtype=origin_var.dtype,
                    persistable=True)
                load_block.append_op(
                    type='load',
                    inputs={},
                    outputs={'Out': [origin]},
                    attrs={
                        'file_path': os.path.join(dirname, origin_var.name)
                    })
T
tangwei12 已提交
216

217 218 219 220 221
        load_block.append_op(
            type='delete_var',
            inputs={'X': need_delete_vars}, )

        executor.run(load_prog)
T
tangwei12 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

    def __load_lookup_table_vars(executor, main_program, lookup_table_var,
                                 lookup_table_var_path):
        emb_var = main_program.global_block().var(lookup_table_var)

        load_program = Program()
        load_block = load_program.global_block()
        load_block.append_op(
            type='load',
            inputs={},
            outputs={'Out': [emb_var]},
            attrs={'file_path': lookup_table_var_path})
        executor.run(load_program)

    if not os.path.isdir(dirname):
        raise ValueError("There is no directory named '%s'", dirname)

    if not os.path.exists(lookup_table_var_path):
        raise ValueError("There is no file named '%s'", lookup_table_var_path)

    if not isinstance(program, Program):
        raise ValueError("program must be an instance of fluid.Program")

    _logger.info("Start Load Sparse Program With "
                 "Distributed Lookup Table Vars from {}, time = {}".format(
                     dirname, time.ctime()))

249 250 251
    need_load_vars = program._parameters_on_pservers.get_distributed_vars_by_ep(
        program._ps_endpoint)
    _load_persistable_vars(executor, dirname, need_load_vars)
T
tangwei12 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    __load_lookup_table_vars(executor, program, lookup_table_var,
                             lookup_table_var_path)

    _logger.info("Finish Load Sparse Program With "
                 "Distributed Lookup Table Vars from {}, time = {}".format(
                     dirname, time.ctime()))


def load_persistables_for_inference(dirname, executor, program,
                                    lookup_table_var_name):
    """
    WARNING: this function will only be used for inference with distributed lookup table.
    Inference with distributed lookup table is a little funky, this function will load distributed
    lookup table vars into sparse var, can be used in local inference mode.

267 268 269 270 271 272 273
    Args:
        dirname(str): The directory path
        executor(Executor): The executor to run for loading inference model.
        program(Program): The parameter server program, which will run on Pserver.
        lookup_table_var_name: the distributed lookup tables var name.
    Returns:
        None
T
tangwei12 已提交
274 275
    """

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    def _load_persistable_vars(executor, dirname, program, lookup_table_vars):
        def _is_checkpoint_var(exclude_fluid_vars=None):
            """
            the checkpoint will not save or load all the variables.
            var type is FEED_MINIBATCH/FETCH_LIST/RAW or var name ends with @GRAD are discarded.

            : param var(Variable)
            """

            if exclude_fluid_vars is None:
                exclude_fluid_vars = []

            def is_valid(var):
                if var.desc.type() == core.VarDesc.VarType.FEED_MINIBATCH or \
                        var.desc.type() == core.VarDesc.VarType.FETCH_LIST or \
                        var.desc.type() == core.VarDesc.VarType.RAW:
                    return False
                # @GRAD are named for gradient variables, checkpoint will not save it.
                if "@GRAD" in var.name:
                    return False
                # .trainer_ are named for distribute train variables, checkpoint will not save it.
                if ".trainer_" in var.name:
                    return False

                # .block is named for distribute train variables, checkpoint will not save it.
                if ".block" in var.name:
                    return False

                if "tmp_" in var.name:
                    return False

                if var.name in exclude_fluid_vars:
                    return False

                return var.persistable

            return is_valid

        io.load_vars(
            executor,
            dirname=dirname,
            main_program=program,
            predicate=_is_checkpoint_var(lookup_table_vars),
            filename=None)

    def _load_lookup_table_vars(executor, dirname, main_program,
                                lookup_table_vars):
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        if not os.path.isdir(dirname):
            raise ValueError("There is no directory named '%s'", dirname)

        lookup_table_dirname = os.path.join(dirname, lookup_table_dir)

        emb_var_name = lookup_table_vars[0]
        emb_var = main_program.global_block().var(emb_var_name)

        emb_files = []
        for emb_name in os.listdir(lookup_table_dirname):
            if emb_var_name in emb_name:
                emb_files.append(emb_name)

        convert_program = Program()
        global_block = convert_program.global_block()

        emb_var = global_block.create_var(
            name=emb_var.name,
            shape=emb_var.shape,
            dtype=emb_var.dtype,
            type=core.VarDesc.VarType.SELECTED_ROWS,
            persistable=True)
        emb_var.desc.set_type(core.VarDesc.VarType.SELECTED_ROWS)

        sums = []

        for i, emb_file in enumerate(emb_files):
            var_name = "{}_{}".format(emb_var.name, i)
            param_var = global_block.create_var(
                name=var_name,
                shape=emb_var.shape,
                dtype=emb_var.dtype,
                type=core.VarDesc.VarType.SELECTED_ROWS,
                persistable=True)
            param_var.desc.set_type(core.VarDesc.VarType.SELECTED_ROWS)
            global_block.append_op(
                type='load',
                inputs={},
                outputs={'Out': [param_var]},
                attrs={
                    'file_path': os.path.join(lookup_table_dirname, var_name)
                })
            sums.append(param_var)
        global_block.append_op(
367 368 369 370 371 372 373 374 375 376 377
            type='merge_sparse_lookup_table',
            inputs={"X": sums},
            outputs={'Out': emb_var},
            attrs={})
        global_block.append_op(
            type='save',
            inputs={"X": [emb_var]},
            outputs={},
            attrs={
                'file_path': os.path.join(lookup_table_dirname, emb_var.name)
            })
378 379 380 381 382 383
        global_block.append_op(type='delete_var', inputs={'X': sums})
        executor.run(convert_program)

    if not os.path.isdir(dirname):
        raise ValueError("There is no directory named '%s'", dirname)

T
tangwei12 已提交
384 385 386 387 388
    if program:
        if not isinstance(program, Program):
            raise ValueError("program must be an instance of fluid.Program")
    else:
        local_model = os.path.join(dirname, model_filename)
389

T
tangwei12 已提交
390 391
        with open(local_model, "rb") as f:
            program_desc_str = f.read()
392

T
tangwei12 已提交
393
        program = Program.parse_from_string(program_desc_str)
394

T
tangwei12 已提交
395 396 397
        if not core._is_program_version_supported(program._version()):
            raise ValueError("Unsupported program version: %d\n" %
                             program._version())
398

T
tangwei12 已提交
399 400 401 402 403
    _logger.info("Start Load Sparse Program With "
                 "Distributed Lookup Table Vars from {}, time = {}".format(
                     dirname, time.ctime()))

    _load_persistable_vars(executor, dirname, program, [lookup_table_var_name])
404
    _load_lookup_table_vars(executor, dirname, program, [lookup_table_var_name])
405

T
tangwei12 已提交
406 407 408
    _logger.info("Finish Load Sparse Program With "
                 "Distributed Lookup Table Vars from {}, time = {}".format(
                     dirname, time.ctime()))
409

T
tangwei12 已提交
410
    return program
411 412 413 414 415 416 417 418 419


def get_inference_model(main_program, feeded_var_names, target_vars):
    """
    Prune the given `main_program` to build a new program especially for inference with distributed lookup table ,
    and then add `feeded_vars` and `target_vars` in this program.

    Args:
        main_program(Program|None): The original program, which will be pruned to
T
tianshuo78520a 已提交
420
                                    build the inference model. If is set None,
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
                                    the default main program will be used.
                                    Default: None.
        feeded_var_names(list[str]): Names of variables that need to be feeded data
                                     during inference.
        target_vars(list[Variable]): Variables from which we can get inference
                                     results.
    Returns:
        program(Program)

    Raises:
        ValueError: If `feed_var_names` is not a list of basestring.
        ValueError: If `target_vars` is not a list of Variable.

    """

    def prepend_feed_ops(inference_program,
                         feed_target_names,
                         feed_holder_name='feed'):
        if len(feed_target_names) == 0:
            return

        global_block = inference_program.global_block()

        feed_var = global_block.create_var(
            name=feed_holder_name,
            type=core.VarDesc.VarType.FEED_MINIBATCH,
            persistable=True)

        for i, name in enumerate(feed_target_names):
            out = global_block.var(name)
            global_block._prepend_op(
                type='feed',
                inputs={'X': [feed_var]},
                outputs={'Out': [out]},
                attrs={'col': i})

    def append_fetch_ops(inference_program,
                         fetch_target_names,
                         fetch_holder_name='fetch'):
        global_block = inference_program.global_block()
        fetch_var = global_block.create_var(
            name=fetch_holder_name,
            type=core.VarDesc.VarType.FETCH_LIST,
            persistable=True)

        for i, name in enumerate(fetch_target_names):
            global_block.append_op(
                type='fetch',
                inputs={'X': [name]},
                outputs={'Out': [fetch_var]},
                attrs={'col': i})

    origin_program = main_program.clone()
    main_program = main_program.clone()
    global_block = main_program.global_block()

    need_to_remove_op_index = []
    for i, op in enumerate(global_block.ops):
        op.desc.set_is_target(False)
        if op.type == "feed" or op.type == "fetch":
            need_to_remove_op_index.append(i)

    for index in need_to_remove_op_index[::-1]:
        global_block._remove_op(index)

    main_program.desc.flush()

    main_program = main_program._prune(targets=target_vars)
    main_program = main_program._inference_optimize(prune_read_op=True)

    fetch_var_names = [v.name for v in target_vars]

    prepend_feed_ops(main_program, feeded_var_names)
    append_fetch_ops(main_program, fetch_var_names)

    return main_program