downpour.py 4.5 KB
Newer Older
D
dongdaxiang 已提交
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

D
dongdaxiang 已提交
14 15
from .node import DownpourServer
from .node import DownpourWorker
D
dongdaxiang 已提交
16 17
from ..backward import append_backward
import ps_pb2 as pslib
D
dongdaxiang 已提交
18
from paddle.fluid.distribute_lookup_table import find_distributed_lookup_table
19 20
from paddle.fluid.distribute_lookup_table import find_distributed_lookup_table_inputs
from paddle.fluid.distribute_lookup_table import find_distributed_lookup_table_outputs
D
dongdaxiang 已提交
21
from google.protobuf import text_format
D
dongdaxiang 已提交
22

23

D
dongdaxiang 已提交
24
class DownpourSGD(object):
25 26 27 28 29 30 31 32 33 34 35 36 37 38
    """
    Distributed optimizer of downpour stochastic gradient descent
    Standard implementation of Google's Downpour SGD
    in Large Scale Distributed Deep Networks

    Args:
        learning_rate (float): the learning rate used to update parameters. \
        Can be a float value
    Examples:
        .. code-block:: python
    
             downpour_sgd = fluid.distributed.DownpourSGD(learning_rate=0.2)
             downpour_sgd.minimize(cost)
    """
39

D
dongdaxiang 已提交
40
    def __init__(self, learning_rate=0.001, window=1):
41 42
        # todo(guru4elephant): add more optimizers here as argument
        # todo(guru4elephant): make learning_rate as a variable
D
dongdaxiang 已提交
43
        self.learning_rate_ = learning_rate
D
dongdaxiang 已提交
44
        self.window_ = window
45
        self.type = "downpour"
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

    def minimize(self,
                 loss,
                 startup_program=None,
                 parameter_list=None,
                 no_grad_set=None):
        """
        DownpounSGD is a distributed optimizer so
        that user can call minimize to generate backward
        operators and optimization operators within minmize function
        Args:
            loss(Variable): loss variable defined by user
            startup_program(Program): startup program that defined by user
            parameter_list(str list): parameter names defined by users
            no_grad_set(set): a set of variables that is defined by users
            so that these variables do not need gradient computation
        Returns:
            [ps_param, worker_skipped_ops]
            ps_param: parameter server protobuf desc
            worker_skipped_ops: operator names that need
            to be skipped during execution
        """
        params_grads = sorted(
            append_backward(loss, parameter_list, no_grad_set),
            key=lambda x: x[0].name)
D
dongdaxiang 已提交
71
        table_name = find_distributed_lookup_table(loss.block.program)
72 73 74 75
        prefetch_slots = find_distributed_lookup_table_inputs(
            loss.block.program, table_name)
        prefetch_slots_emb = find_distributed_lookup_table_outputs(
            loss.block.program, table_name)
D
dongdaxiang 已提交
76
        server = DownpourServer()
77
        # window is communication strategy
D
dongdaxiang 已提交
78
        worker = DownpourWorker(self.window_)
79 80 81 82 83
        # Todo(guru4elephant): support multiple tables definitions
        # currently support one big sparse table
        sparse_table_index = 0
        # currently merge all dense parameters into one dense table
        dense_table_index = 1
H
heqiaozhi 已提交
84 85 86 87 88 89
        params = []
        grads = []
        for i in params_grads:
            params.append(i[0])
        for i in params_grads:
            grads.append(i[1])
90
        server.add_sparse_table(sparse_table_index, self.learning_rate_,
D
dongdaxiang 已提交
91
                                prefetch_slots, prefetch_slots_emb)
92 93
        server.add_dense_table(dense_table_index, self.learning_rate_, params,
                               grads)
94
        worker.add_sparse_table(sparse_table_index, self.learning_rate_,
D
dongdaxiang 已提交
95
                                prefetch_slots, prefetch_slots_emb)
96 97
        worker.add_dense_table(dense_table_index, self.learning_rate_, params,
                               grads)
D
dongdaxiang 已提交
98 99
        ps_param = pslib.PSParameter()
        ps_param.server_param.CopyFrom(server.get_desc())
H
heqiaozhi 已提交
100
        ps_param.trainer_param.CopyFrom(worker.get_desc())
101 102
        # Todo(guru4elephant): figure out how to support more sparse parameters
        # currently only support lookup_table
D
dongdaxiang 已提交
103
        worker_skipped_ops = ["lookup_table", "lookup_table_grad"]
H
heqiaozhi 已提交
104
        ps_param.trainer_param.skip_op.extend(worker_skipped_ops)
H
heqiaozhi 已提交
105
        return [ps_param, worker_skipped_ops]