device_worker.py 5.6 KB
Newer Older
1
#   Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13
#
# 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.
D
dongdaxiang 已提交
14
import sys
15

16 17
__all__ = ['DeviceWorker', 'Hogwild', 'DownpourSGD']

18 19

class DeviceWorker(object):
X
xjqbest 已提交
20 21 22
    """
    DeviceWorker is a abstract class, which generates worker desc.
    """
23
    def __init__(self):
X
xjqbest 已提交
24 25 26
        """
        Init.
        """
D
dongdaxiang 已提交
27
        self.program_ = None
28 29 30 31
        self.infer_ = None

    def set_infer(self, infer=False):
        self.infer_ = infer
D
dongdaxiang 已提交
32 33

    def set_fleet_desc(self, fleet_desc):
X
xjqbest 已提交
34 35 36 37 38 39
        """
        Set fleet desc.

        Args:
            fleet_desc(PSParameter): pslib.PSParameter object
        """
D
dongdaxiang 已提交
40 41 42
        self.fleet_desc_ = fleet_desc

    def set_program(self, program):
X
xjqbest 已提交
43 44 45 46 47 48
        """
        Set program.

        Args:
            program(Program): a Program object
        """
D
dongdaxiang 已提交
49
        self.program_ = program
50

51
    def gen_worker_desc(self, trainer_desc):
X
xjqbest 已提交
52 53 54 55 56 57 58 59 60
        """
        Generator worker desc.

        Args:
            trainer_desc(TrainerDesc): a TrainerDesc object
        """
        raise NotImplementedError(
            "DeviceWorker does not implement gen_worker_desc, "
            "please use Hogwild or DownpourSGD, etc.")
61 62 63


class Hogwild(DeviceWorker):
X
xjqbest 已提交
64 65 66 67
    """
    Hogwild is a kind of SGD algorithm.

    """
68
    def __init__(self):
X
xjqbest 已提交
69 70 71
        """
        Init.
        """
72 73
        super(Hogwild, self).__init__()

74
    def gen_worker_desc(self, trainer_desc):
X
xjqbest 已提交
75 76 77 78 79 80
        """
        Generator worker desc, which device worker is HogwildWorker.

        Args:
            trainer_desc(TrainerDesc): a TrainerDesc object
        """
81 82 83
        trainer_desc.device_worker_name = "HogwildWorker"


D
dongdaxiang 已提交
84
class DownpourSGD(DeviceWorker):
X
xjqbest 已提交
85 86 87
    """
    DownpourSGD is a kind of distributed SGD algorithm.
    """
88
    def __init__(self):
X
xjqbest 已提交
89 90 91
        """
        Init.
        """
D
dongdaxiang 已提交
92
        super(DownpourSGD, self).__init__()
93

94
    def gen_worker_desc(self, trainer_desc):
X
xjqbest 已提交
95 96 97 98 99 100
        """
        Generator worker desc, which device worker is DownpourWorker.

        Args:
            trainer_desc(TrainerDesc): a TrainerDesc object
        """
X
fix bug  
xjqbest 已提交
101
        dense_table_set = set()
D
dongdaxiang 已提交
102 103 104 105 106 107
        program_id = str(id(self.program_))
        if self.program_ == None:
            print("program of current device worker is not configured")
            sys.exit(-1)
        opt_info = self.program_._fleet_opt
        program_configs = opt_info["program_configs"]
108
        downpour = trainer_desc.downpour_param
D
dongdaxiang 已提交
109

D
dongdaxiang 已提交
110 111
        for pid in program_configs:
            if pid == program_id:
D
dongdaxiang 已提交
112 113 114 115 116 117
                pc = downpour.program_config.add()
                pc.program_id = program_id
                for i in program_configs[program_id]["push_sparse"]:
                    pc.push_sparse_table_id.extend([i])
                for i in program_configs[program_id]["push_dense"]:
                    pc.push_dense_table_id.extend([i])
X
xjqbest 已提交
118
                    dense_table_set.add(i)
D
dongdaxiang 已提交
119 120 121 122
                for i in program_configs[program_id]["pull_sparse"]:
                    pc.pull_sparse_table_id.extend([i])
                for i in program_configs[program_id]["pull_dense"]:
                    pc.pull_dense_table_id.extend([i])
X
fix bug  
xjqbest 已提交
123
                    dense_table_set.add(i)
D
dongdaxiang 已提交
124
                break
125

126 127 128 129 130 131
        trainer_desc.device_worker_name = "DownpourWorker"
        pull_thread = trainer_desc.pull_dense_param
        pull_thread.device_num = trainer_desc.thread_num
        for i in self.fleet_desc_.trainer_param.dense_table:
            if i.table_id in dense_table_set:
                dense_table = pull_thread.dense_table.add()
132
                dense_table.dense_value_name.extend(i.dense_variable_name)
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                dense_table.table_id = \
                    i.table_id
        sparse_table = downpour.sparse_table.add()
        sparse_table.table_id = \
                    self.fleet_desc_.trainer_param.sparse_table[0].table_id
        sparse_table.sparse_key_name.extend(
            self.fleet_desc_.trainer_param.sparse_table[0].slot_key)
        sparse_table.sparse_value_name.extend(
            self.fleet_desc_.trainer_param.sparse_table[0].slot_value)
        sparse_table.sparse_grad_name.extend(
            self.fleet_desc_.trainer_param.sparse_table[0].slot_gradient)
        sparse_table.emb_dim = \
                    self.fleet_desc_.server_param.downpour_server_param.downpour_table_param[
                        0].accessor.fea_dim - 2
        sparse_table.fea_dim = sparse_table.emb_dim + 2
        # TODO(guru4elephant): hard code here, need to improve
        sparse_table.label_var_name = "click"

X
fix bug  
xjqbest 已提交
151 152 153 154
        for i in self.fleet_desc_.trainer_param.dense_table:
            if i.table_id in dense_table_set:
                dense_table = downpour.dense_table.add()
                dense_table.table_id = i.table_id
155
                dense_table.dense_value_name.extend(i.dense_variable_name)
X
fix bug  
xjqbest 已提交
156 157 158
                dense_table.dense_grad_name.extend(
                    i.dense_gradient_variable_name)
                downpour.skip_ops.extend(self.fleet_desc_.trainer_param.skip_op)
159 160 161
        if self.infer_:
            downpour.push_dense = False
            downpour.push_sparse = False
X
fix bug  
xjqbest 已提交
162

163 164 165 166 167

class DeviceWorkerFactory(object):
    def create_device_worker(self, worker_type):
        classname = worker_type.capitalize()
        return globals()[classname]()