device_worker.py 18.2 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.
14
"""Defination of device workers."""
15

C
Chengmo 已提交
16 17
from __future__ import print_function

18 19 20
__all__ = [
    'DeviceWorker', 'Hogwild', 'DownpourSGD', 'Section', 'DownpourSGDOPT'
]
21

22 23

class DeviceWorker(object):
X
xjqbest 已提交
24
    """
25
    DeviceWorker is an abstract class, which generates worker desc.
26 27
    This class is an inner class that we do computation logics within
    the implementation. For example, execution of a program or a graph.
X
xjqbest 已提交
28
    """
29

30
    def __init__(self):
31
        """Init."""
D
dongdaxiang 已提交
32 33
        self._program = None
        self._infer = None
34

35 36 37
    def _set_infer(self, infer=False):
        """
        set inference flag for current device worker
C
Chengmo 已提交
38

39 40 41
        Args:
            infer(bool): whether to do inference
        """
D
dongdaxiang 已提交
42
        self._infer = infer
D
dongdaxiang 已提交
43

44
    def _set_fleet_desc(self, fleet_desc):
X
xjqbest 已提交
45 46 47 48 49 50
        """
        Set fleet desc.

        Args:
            fleet_desc(PSParameter): pslib.PSParameter object
        """
D
dongdaxiang 已提交
51
        self._fleet_desc = fleet_desc
D
dongdaxiang 已提交
52

53
    def _set_program(self, program):
X
xjqbest 已提交
54 55 56 57 58 59
        """
        Set program.

        Args:
            program(Program): a Program object
        """
D
dongdaxiang 已提交
60
        self._program = program
61

62
    def _gen_worker_desc(self, trainer_desc):
X
xjqbest 已提交
63 64 65 66 67 68 69 70 71
        """
        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.")
72 73 74


class Hogwild(DeviceWorker):
X
xjqbest 已提交
75 76 77 78
    """
    Hogwild is a kind of SGD algorithm.

    """
79

80
    def __init__(self):
81
        """Init."""
82 83
        super(Hogwild, self).__init__()

84
    def _gen_worker_desc(self, trainer_desc):
X
xjqbest 已提交
85 86 87 88 89 90
        """
        Generator worker desc, which device worker is HogwildWorker.

        Args:
            trainer_desc(TrainerDesc): a TrainerDesc object
        """
91
        trainer_desc.device_worker_name = "HogwildWorker"
D
dongdaxiang 已提交
92
        if self._infer:
93 94
            # just ignore feed op for inference model
            trainer_desc.hogwild_param.skip_ops.extend(["feed"])
95

96 97 98 99 100 101
        dense_table_set = set()
        program_id = str(id(self._program))
        if self._program == None:
            print("program of current device worker is not configured")
            exit(-1)
        opt_info = self._program._fleet_opt
102 103
        # when opt_info is None or empty dict, it should return
        if not opt_info:
104 105
            return

106 107
        from paddle.fluid.incubate.fleet.parameter_server import version

C
Chengmo 已提交
108 109 110
        if version.is_transpiler() and "fleet_desc" not in opt_info:
            return

111 112
        program_configs = opt_info["program_configs"]
        downpour = trainer_desc.downpour_param
113
        hogwild = trainer_desc.hogwild_param
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

        for pid in program_configs:
            if pid == program_id:
                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])
                    dense_table_set.add(i)
                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])
                    dense_table_set.add(i)
                break

        trainer_desc.device_worker_name = "HogwildWorker"
        pull_thread = trainer_desc.pull_dense_param
        pull_thread.device_num = trainer_desc.thread_num
        if opt_info.get("program_id_to_worker") is None:
            raise ValueError("opt_info must have program_id_to_worker")
        prog_id_to_worker = opt_info["program_id_to_worker"]
        if prog_id_to_worker.get(program_id) is None:
            raise ValueError("%s not found in program_id_to_worker" %
                             program_id)
        worker = opt_info["program_id_to_worker"][program_id]
        for i in worker.get_desc().dense_table:
            if i.table_id in dense_table_set:
                dense_table = pull_thread.dense_table.add()
                dense_table.dense_value_name.extend(i.dense_variable_name)
                dense_table.table_id = \
                    i.table_id
        sparse_len = len(worker.get_desc().sparse_table)
        for i in range(sparse_len):
            sparse_table = downpour.sparse_table.add()
            sparse_table.table_id = worker.get_desc().sparse_table[i].table_id
            sparse_table.sparse_key_name.extend(worker.get_desc().sparse_table[
                i].slot_key)
            sparse_table.sparse_value_name.extend(worker.get_desc()
                                                  .sparse_table[i].slot_value)
            sparse_table.sparse_grad_name.extend(worker.get_desc().sparse_table[
                i].slot_gradient)
            sparse_table.fea_dim = \
                self._fleet_desc.server_param.downpour_server_param.downpour_table_param[
C
Chengmo 已提交
159
                    i].accessor.fea_dim
160 161 162 163 164 165
            # not use emb_dim
            sparse_table.emb_dim = -1
            # not use hard code click
            sparse_table.label_var_name = ""
        if opt_info["stat_var_names"]:
            for i in opt_info["stat_var_names"]:
166
                hogwild.stat_var_names.extend([i])
167 168 169 170 171 172 173 174 175
                downpour.stat_var_names.extend([i])

        for i in worker.get_desc().dense_table:
            if i.table_id in dense_table_set:
                dense_table = downpour.dense_table.add()
                dense_table.table_id = i.table_id
                dense_table.dense_value_name.extend(i.dense_variable_name)
                dense_table.dense_grad_name.extend(
                    i.dense_gradient_variable_name)
176
        hogwild.skip_ops.extend(worker.get_desc().skip_op)
177
        if self._infer:
178 179
            hogwild.skip_ops.extend(
                ["push_sparse", "push_sparse_v2", "push_dense"])
180

181

D
dongdaxiang 已提交
182
class DownpourSGD(DeviceWorker):
X
xjqbest 已提交
183 184 185
    """
    DownpourSGD is a kind of distributed SGD algorithm.
    """
186

187
    def __init__(self):
X
xjqbest 已提交
188 189
        """
        Init.
190
        initialize downpourSGD device worker
X
xjqbest 已提交
191
        """
D
dongdaxiang 已提交
192
        super(DownpourSGD, self).__init__()
193

194
    def _gen_worker_desc(self, trainer_desc):
X
xjqbest 已提交
195 196 197 198 199 200
        """
        Generator worker desc, which device worker is DownpourWorker.

        Args:
            trainer_desc(TrainerDesc): a TrainerDesc object
        """
X
fix bug  
xjqbest 已提交
201
        dense_table_set = set()
D
dongdaxiang 已提交
202 203
        program_id = str(id(self._program))
        if self._program == None:
D
dongdaxiang 已提交
204
            print("program of current device worker is not configured")
205
            exit(-1)
D
dongdaxiang 已提交
206
        opt_info = self._program._fleet_opt
D
dongdaxiang 已提交
207
        program_configs = opt_info["program_configs"]
208
        downpour = trainer_desc.downpour_param
D
dongdaxiang 已提交
209

D
dongdaxiang 已提交
210 211
        for pid in program_configs:
            if pid == program_id:
D
dongdaxiang 已提交
212 213 214 215 216 217
                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 已提交
218
                    dense_table_set.add(i)
D
dongdaxiang 已提交
219 220 221 222
                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 已提交
223
                    dense_table_set.add(i)
Z
zhang wenhui 已提交
224 225 226 227 228 229 230
                # code for partial push dense table such as multitask
                if "cond2denseid" in program_configs[program_id]:
                    cond2denseid = program_configs[program_id]["cond2denseid"]
                    for key, value in cond2denseid.items():
                        mc_map = pc.partial_pushdense_condtable_map.add()
                        mc_map.key = key
                        mc_map.value = value
D
dongdaxiang 已提交
231
                break
232

T
Thunderbrook 已提交
233 234
        trainer_desc.device_worker_name = opt_info.get("worker_class",
                                                       "DownpourWorker")
235 236
        pull_thread = trainer_desc.pull_dense_param
        pull_thread.device_num = trainer_desc.thread_num
237 238 239 240 241 242 243 244
        if opt_info.get("program_id_to_worker") is None:
            raise ValueError("opt_info must have program_id_to_worker")
        prog_id_to_worker = opt_info["program_id_to_worker"]
        if prog_id_to_worker.get(program_id) is None:
            raise ValueError("%s not found in program_id_to_worker" %
                             program_id)
        worker = opt_info["program_id_to_worker"][program_id]
        for i in worker.get_desc().dense_table:
245 246
            if i.table_id in dense_table_set:
                dense_table = pull_thread.dense_table.add()
247
                dense_table.dense_value_name.extend(i.dense_variable_name)
248 249
                dense_table.table_id = \
                    i.table_id
250
        sparse_len = len(worker.get_desc().sparse_table)
251 252
        for i in range(sparse_len):
            sparse_table = downpour.sparse_table.add()
253 254 255 256 257 258 259
            sparse_table.table_id = worker.get_desc().sparse_table[i].table_id
            sparse_table.sparse_key_name.extend(worker.get_desc().sparse_table[
                i].slot_key)
            sparse_table.sparse_value_name.extend(worker.get_desc()
                                                  .sparse_table[i].slot_value)
            sparse_table.sparse_grad_name.extend(worker.get_desc().sparse_table[
                i].slot_gradient)
260 261
            if opt_info["use_cvm"] or "no_cvm" in opt_info and opt_info[
                    "no_cvm"] == True:
262 263
                sparse_table.emb_dim = \
                    self._fleet_desc.server_param.downpour_server_param.downpour_table_param[
C
Chengmo 已提交
264
                        i].accessor.fea_dim
265 266 267 268
                sparse_table.fea_dim = sparse_table.emb_dim
            else:
                sparse_table.emb_dim = \
                    self._fleet_desc.server_param.downpour_server_param.downpour_table_param[
C
Chengmo 已提交
269
                        i].accessor.fea_dim - 2
270 271 272
                sparse_table.fea_dim = sparse_table.emb_dim + 2
            # TODO(guru4elephant): hard code here, need to improve
            sparse_table.label_var_name = "click"
273 274 275
        if opt_info["stat_var_names"]:
            for i in opt_info["stat_var_names"]:
                downpour.stat_var_names.extend([i])
276

277
        for i in worker.get_desc().dense_table:
X
fix bug  
xjqbest 已提交
278 279 280
            if i.table_id in dense_table_set:
                dense_table = downpour.dense_table.add()
                dense_table.table_id = i.table_id
281
                dense_table.dense_value_name.extend(i.dense_variable_name)
X
fix bug  
xjqbest 已提交
282 283
                dense_table.dense_grad_name.extend(
                    i.dense_gradient_variable_name)
X
xujiaqi01 已提交
284
        downpour.skip_ops.extend(worker.get_desc().skip_op)
D
dongdaxiang 已提交
285
        if self._infer:
286 287
            downpour.push_dense = False
            downpour.push_sparse = False
X
fix bug  
xjqbest 已提交
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 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
class DownpourSGDOPT(DeviceWorker):
    """
    DownpourSGDOPT is a kind of distributed SGD algorithm.
    """

    def __init__(self):
        """
        Init.
        initialize downpourSGDOPT device worker
        """
        super(DownpourSGDOPT, self).__init__()

    def _gen_worker_desc(self, trainer_desc):
        """
        Generator worker desc, which device worker is DownpourWorker.

        Args:
            trainer_desc(TrainerDesc): a TrainerDesc object
        """
        dense_table_set = set()
        program_id = str(id(self._program))
        if self._program == None:
            print("program of current device worker is not configured")
            exit(-1)
        opt_info = self._program._fleet_opt
        program_configs = opt_info["program_configs"]
        downpour = trainer_desc.downpour_param

        for pid in program_configs:
            if pid == program_id:
                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])
                    dense_table_set.add(i)
                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])
                    dense_table_set.add(i)
                break

        trainer_desc.device_worker_name = "DownpourWorkerOpt"
        pull_thread = trainer_desc.pull_dense_param
        pull_thread.device_num = trainer_desc.thread_num
        if opt_info.get("program_id_to_worker") is None:
            raise ValueError("opt_info must have program_id_to_worker")
        prog_id_to_worker = opt_info["program_id_to_worker"]
        if prog_id_to_worker.get(program_id) is None:
            raise ValueError("%s not found in program_id_to_worker" %
                             program_id)
        worker = opt_info["program_id_to_worker"][program_id]
        for i in worker.get_desc().dense_table:
            if i.table_id in dense_table_set:
                dense_table = pull_thread.dense_table.add()
                dense_table.dense_value_name.extend(i.dense_variable_name)
                dense_table.table_id = \
                    i.table_id
        sparse_len = len(worker.get_desc().sparse_table)
        for i in range(sparse_len):
            sparse_table = downpour.sparse_table.add()
            sparse_table.table_id = worker.get_desc().sparse_table[i].table_id
            sparse_table.sparse_key_name.extend(worker.get_desc().sparse_table[
                i].slot_key)
            sparse_table.sparse_value_name.extend(worker.get_desc()
                                                  .sparse_table[i].slot_value)
            sparse_table.sparse_grad_name.extend(worker.get_desc().sparse_table[
                i].slot_gradient)
            if opt_info["use_cvm"] or "no_cvm" in opt_info and opt_info[
                    "no_cvm"] == True:
                sparse_table.emb_dim = \
                    self._fleet_desc.server_param.downpour_server_param.downpour_table_param[
C
Chengmo 已提交
364
                        i].accessor.fea_dim
365 366 367 368
                sparse_table.fea_dim = sparse_table.emb_dim
            else:
                sparse_table.emb_dim = \
                    self._fleet_desc.server_param.downpour_server_param.downpour_table_param[
C
Chengmo 已提交
369
                        i].accessor.fea_dim - 2
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
                sparse_table.fea_dim = sparse_table.emb_dim + 2
            # TODO(guru4elephant): hard code here, need to improve
            sparse_table.label_var_name = "click"
        if "local_tables" in opt_info and sparse_table.table_id in opt_info[
                "local_tables"]:
            sparse_table.is_local = True
        if "async_tables" in opt_info and sparse_table.table_id in opt_info[
                "async_tables"]:
            sparse_table.is_async = True
        if opt_info["stat_var_names"]:
            for i in opt_info["stat_var_names"]:
                downpour.stat_var_names.extend([i])

        for i in worker.get_desc().dense_table:
            if i.table_id in dense_table_set:
                dense_table = downpour.dense_table.add()
                dense_table.table_id = i.table_id
                dense_table.dense_value_name.extend(i.dense_variable_name)
                dense_table.dense_grad_name.extend(
                    i.dense_gradient_variable_name)
        downpour.skip_ops.extend(worker.get_desc().skip_op)
        if self._infer:
            downpour.push_dense = False
            downpour.push_sparse = False


H
hutuxian 已提交
396
class Section(DeviceWorker):
397
    """SectionWorker."""
H
hutuxian 已提交
398 399

    def __init__(self):
400
        """Init."""
H
hutuxian 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413
        super(Section, self).__init__()

    def _gen_worker_desc(self, trainer_desc):
        """
        Generator worker desc, which device worker is SectionWorker.
        Args:
            trainer_desc(TrainerDesc): a TrainerDesc object
        """
        from google.protobuf import text_format
        from . import core
        trainer_desc.device_worker_name = "SectionWorker"
        pipeline_opt = self._program._pipeline_opt
        section_param = trainer_desc.section_param
L
lilong12 已提交
414
        section_param.num_microbatches = pipeline_opt["num_microbatches"]
H
hutuxian 已提交
415
        section_param.start_cpu_core_id = pipeline_opt["start_cpu_core_id"]
416 417 418 419 420 421 422 423 424 425 426 427
        section_param.pipeline_stage = pipeline_opt["pipeline_stage"]
        section_param.num_pipeline_stages = pipeline_opt["num_pipeline_stages"]
        schedule_mode_str = pipeline_opt["schedule_mode"]
        # F-then-B scheduler which runs Forward phase for all microbatches,
        # then runs Backward phase for all microbatches.
        # 1F1B scheduler, which runs forward phase and backward phase altertively
        # after startup phase.
        assert schedule_mode_str in ["F-then-B", "1F1B"], (
            "The schedule mode "
            "for pipeline must be one of F-then-B or 1F1B")
        schedule_mode = 0 if schedule_mode_str == "F-then-B" else 1
        section_param.schedule_mode = schedule_mode
428 429 430 431 432 433 434 435 436 437 438
        cfg = section_param.section_config
        program = pipeline_opt["section_program"]
        cfg.program_desc.ParseFromString(program["program"]._get_desc()
                                         .serialize_to_string())
        # TODO: why does not work
        # cfg.program_desc.CopyFrom(program.program._get_desc())
        place = pipeline_opt["place"]
        place_id = pipeline_opt["place_id"]
        assert isinstance(place, core.CUDAPlace)
        cfg.place = cfg.CUDAPlace
        cfg.place_id = place_id
H
hutuxian 已提交
439 440


441
class DeviceWorkerFactory(object):
442
    def _create_device_worker(self, worker_type):
443 444
        classname = worker_type.capitalize()
        return globals()[classname]()