downpour.py 2.0 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):
D
dongdaxiang 已提交
11
    def __init__(self, learning_rate=0.001, window=1):
D
dongdaxiang 已提交
12
        # todo(guru4elephant): if optimizer is not None, will warning here
D
dongdaxiang 已提交
13
        self.learning_rate_ = learning_rate
D
dongdaxiang 已提交
14 15
        self.window_ = window

D
dongdaxiang 已提交
16
    def minimize(self, loss, startup_program=None,
17
                 parameter_list=None, no_grad_set=None):
D
dongdaxiang 已提交
18
        params_grads = sorted(append_backward(loss), key=lambda x:x[0].name)
D
dongdaxiang 已提交
19
        table_name = find_distributed_lookup_table(loss.block.program)
20 21 22 23
        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 已提交
24
        server = DownpourServer()
D
dongdaxiang 已提交
25
        worker = DownpourWorker(self.window_)
26
        server.add_sparse_table(0, self.learning_rate_,
D
dongdaxiang 已提交
27
                                prefetch_slots, prefetch_slots_emb)
28 29
        server.add_dense_table(1, self.learning_rate_, params_grads[0], params_grads[1])
        worker.add_sparse_table(0, self.learning_rate_,
D
dongdaxiang 已提交
30
                                prefetch_slots, prefetch_slots_emb)
31
        worker.add_dense_table(1, self.learning_rate_, params_grads[0], params_grads[1])
D
dongdaxiang 已提交
32 33
        ps_param = pslib.PSParameter()
        ps_param.server_param.CopyFrom(server.get_desc())
D
dongdaxiang 已提交
34
        #ps_param.worker_param.CopyFrom(worker.get_desc())
D
dongdaxiang 已提交
35
        worker_skipped_ops = ["lookup_table", "lookup_table_grad"]
D
dongdaxiang 已提交
36
        ps_param_str = text_format.MessageToString(ps_param)
37
        return [ps_param_str, worker_skipped_ops, text_format.MessageToString(worker.get_desc())]