downpour.py 3.2 KB
Newer Older
D
dongdaxiang 已提交
1 2
from .node import DownpourServer
from .node import DownpourWorker
D
dongdaxiang 已提交
3 4
from ..backward import append_backward
import ps_pb2 as pslib
D
dongdaxiang 已提交
5
from paddle.fluid.distribute_lookup_table import find_distributed_lookup_table
6 7
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 已提交
8
from google.protobuf import text_format
D
dongdaxiang 已提交
9 10

class DownpourSGD(object):
11 12 13 14 15 16 17 18 19 20 21 22 23 24
    """
    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)
    """
D
dongdaxiang 已提交
25
    def __init__(self, learning_rate=0.001, window=1):
26 27
        # todo(guru4elephant): add more optimizers here as argument
        # todo(guru4elephant): make learning_rate as a variable
D
dongdaxiang 已提交
28
        self.learning_rate_ = learning_rate
D
dongdaxiang 已提交
29
        self.window_ = window
30 31
        self.type = "downpour"
    
D
dongdaxiang 已提交
32
    def minimize(self, loss, startup_program=None,
33
                 parameter_list=None, no_grad_set=None):
34 35
        params_grads = sorted(append_backward(
            loss, parameter_list, no_grad_set), key=lambda x:x[0].name)
D
dongdaxiang 已提交
36
        table_name = find_distributed_lookup_table(loss.block.program)
37 38 39 40
        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 已提交
41
        server = DownpourServer()
42
        # window is communication strategy
D
dongdaxiang 已提交
43
        worker = DownpourWorker(self.window_)
44 45 46 47 48
        # 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 已提交
49 50 51 52 53 54
        params = []
        grads = []
        for i in params_grads:
            params.append(i[0])
        for i in params_grads:
            grads.append(i[1])
55
        server.add_sparse_table(sparse_table_index, self.learning_rate_,
D
dongdaxiang 已提交
56
                                prefetch_slots, prefetch_slots_emb)
57
        server.add_dense_table(dense_table_index, self.learning_rate_, 
H
heqiaozhi 已提交
58
                               params, grads)
59
        worker.add_sparse_table(sparse_table_index, self.learning_rate_,
D
dongdaxiang 已提交
60
                                prefetch_slots, prefetch_slots_emb)
61
        worker.add_dense_table(dense_table_index, self.learning_rate_, 
H
heqiaozhi 已提交
62
                               params, grads)
D
dongdaxiang 已提交
63 64
        ps_param = pslib.PSParameter()
        ps_param.server_param.CopyFrom(server.get_desc())
H
heqiaozhi 已提交
65
        ps_param.trainer_param.CopyFrom(worker.get_desc())
66 67
        # Todo(guru4elephant): figure out how to support more sparse parameters
        # currently only support lookup_table
D
dongdaxiang 已提交
68
        worker_skipped_ops = ["lookup_table", "lookup_table_grad"]
D
dongdaxiang 已提交
69
        ps_param_str = text_format.MessageToString(ps_param)
H
heqiaozhi 已提交
70
        return [ps_param, worker_skipped_ops]