sharding_optimizer.py 51.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2020 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
# limitations under the License.

from paddle.fluid import unique_name, core
import paddle.fluid as fluid

from paddle.distributed.fleet.meta_optimizers.common import OpRole, OP_ROLE_VAR_KEY, CollectiveHelper
J
update  
JZ-LIANG 已提交
19
from paddle.distributed.fleet.meta_optimizers.common import is_backward_op, is_optimizer_op, is_update_op
20 21 22 23 24
from paddle.distributed.fleet.meta_optimizers.meta_optimizer_base import MetaOptimizerBase
from paddle.distributed.fleet.meta_optimizers.sharding.shard import Shard, ProgramSegment
from paddle.distributed.fleet.meta_optimizers.sharding.fp16_helper import FP16Utils
from paddle.distributed.fleet.meta_optimizers.sharding.weight_decay_helper import WeightDecayHelper
from paddle.distributed.fleet.meta_optimizers.sharding.gradient_clip_helper import GradientClipHelper
W
update  
WangXi 已提交
25
from .sharding.offload_helper import OffloadHelper
26 27
from paddle.distributed.fleet.meta_optimizers.sharding.prune import ProgramDeps
from paddle.distributed.fleet.meta_optimizers.sharding.utils import *
J
update  
JZ-LIANG 已提交
28

29
import logging
30 31 32 33 34 35
from functools import reduce

__all__ = ["ShardingOptimizer"]


class ShardingOptimizer(MetaOptimizerBase):
S
sandyhouse 已提交
36 37
    """Sharding Optimizer."""

38 39 40 41 42 43
    def __init__(self, optimizer):
        super(ShardingOptimizer, self).__init__(optimizer)
        self.inner_opt = optimizer
        self.meta_optimizers_white_list = [
            "RecomputeOptimizer",
            "AMPOptimizer",
44 45
            "LarsOptimizer",
            "LambOptimizer",
S
update  
sandyhouse 已提交
46
            # "ModelParallelOptimizer",
S
sandyhouse 已提交
47
            "PipelineOptimizer",
48 49 50 51 52 53 54 55 56 57 58 59
        ]
        self.meta_optimizers_black_list = ["GraphExecutionOptimizer", ]
        self._main_program = None
        self._startup_program = None
        self._segments = []
        # params and fp16 params is for broadcast
        self._params = set([])
        self._broadcast_vars = set([])
        # reduced grads to param name
        self._reduced_grads_to_param = {}
        self._shard = Shard()

S
update  
sandyhouse 已提交
60 61 62 63
        # use sharding as outer parallelism (e.g. inner:Megatron & outer sharding)
        self._as_outer_parallelism = False
        self._inner_parallelism_size = None

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    def _can_apply(self):
        if not self.role_maker._is_collective:
            return False
        if self.role_maker._worker_num() <= 1:
            return False
        return self.user_defined_strategy.sharding

    def _disable_strategy(self, dist_strategy):
        dist_strategy.sharding = False
        dist_strategy.sharding_configs = {}

    def _enable_strategy(self, dist_strategy, context):
        dist_strategy.sharding = True
        dist_strategy.sharding_configs = {"fuse_broadcast_MB": 32}

    def minimize_impl(self,
                      loss,
                      startup_program=None,
                      parameter_list=None,
                      no_grad_set=None):
S
sandyhouse 已提交
84
        """Implementation of minimize."""
85 86 87 88
        # TODO: (JZ-LIANG) support multiple comm in future
        # self._nrings = self.user_defined_strategy.nccl_comm_num
        self._nrings_sharding = 1
        self._nrings_dp = 1
89 90
        self._fuse_broadcast_MB = self.user_defined_strategy.sharding_configs[
            "fuse_broadcast_MB"]
91 92
        self.hybrid_dp = self.user_defined_strategy.sharding_configs[
            "hybrid_dp"]
S
update  
sandyhouse 已提交
93 94 95
        self._as_outer_parallelism = self.user_defined_strategy.sharding_configs[
            "as_outer_parallelism"]
        self._inner_parallelism_size = int(
S
sandyhouse 已提交
96
            self.user_defined_strategy.sharding_configs["parallelism"])
S
update  
sandyhouse 已提交
97 98
        self.use_pipeline = self.user_defined_strategy.sharding_configs[
            "use_pipeline"]
S
sandyhouse 已提交
99 100
        self.acc_steps = self.user_defined_strategy.sharding_configs[
            "acc_steps"]
S
update  
sandyhouse 已提交
101 102
        self.schedule_mode = self.user_defined_strategy.sharding_configs[
            "schedule_mode"]
S
sandyhouse 已提交
103
        self.pp_bz = self.user_defined_strategy.sharding_configs["pp_bz"]
R
update  
root 已提交
104 105
        self.pp_allreduce_in_optimize = self.user_defined_strategy.sharding_configs[
            "pp_allreduce_in_optimize"]
106 107 108 109

        if self.inner_opt is None:
            raise ValueError(
                "self.inner_opt of ShardingOptimizer should not be None.")
S
update  
sandyhouse 已提交
110
        if self.use_pipeline:
S
sandyhouse 已提交
111 112
            pp_optimizer = fluid.optimizer.PipelineOptimizer(self.inner_opt,
                                                             self.acc_steps)
S
update  
sandyhouse 已提交
113 114
            main_program = loss.block.program
            main_program._pipeline_opt = dict()
S
update  
sandyhouse 已提交
115
            main_program._pipeline_opt['schedule_mode'] = self.schedule_mode
S
sandyhouse 已提交
116
            main_program._pipeline_opt['pp_bz'] = self.pp_bz
S
sandyhouse 已提交
117 118 119
            pp_rank = self.role_maker._worker_index() // (
                self.user_defined_strategy.sharding_configs[
                    'sharding_group_size'] * self._inner_parallelism_size)
S
update  
sandyhouse 已提交
120 121 122 123
            main_program._pipeline_opt['local_rank'] = pp_rank
            main_program._pipeline_opt[
                'global_rank'] = self.role_maker._worker_index()
            main_program._pipeline_opt['use_sharding'] = True
S
update  
sandyhouse 已提交
124 125
            main_program._pipeline_opt['ring_id'] = 20
            optimize_ops, params_grads, program_list, self.pipeline_pair, self.pp_ring_map = pp_optimizer.minimize(
S
update  
sandyhouse 已提交
126 127 128 129 130
                loss, startup_program, parameter_list, no_grad_set)
            self.pipeline_nodes = len(program_list)
        else:
            optimize_ops, params_grads = self.inner_opt.minimize(
                loss, startup_program, parameter_list, no_grad_set)
131 132 133

        if startup_program is None:
            startup_program = default_startup_program()
S
update  
sandyhouse 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        if self.use_pipeline:
            startup_program = startup_program._pipeline_opt['startup_program']
            #main_program = main_program._pipeline_opt['section_program']['program']
            print("pp_rank:", pp_rank)
            main_program = program_list[pp_rank]['program']
            with open("main_%d" % self.role_maker._worker_index(), 'w') as f:
                f.writelines(str(main_program))
            main_block = main_program.global_block()
            new_params_grads = []
            for param, grad in params_grads:
                if main_block.has_var(param.name):
                    new_params_grads.append((param, grad))
            params_grads = new_params_grads

        else:
            main_block = loss.block
150 151 152 153
        startup_block = startup_program.global_block()
        self._main_program = main_block.program
        self._startup_program = startup_program

S
update  
sandyhouse 已提交
154 155 156
        if self.use_pipeline:
            pp_optimizer._rename_gradient_var_name(main_block)

157 158 159 160 161 162 163 164 165 166 167 168
        # step1: set_up
        self._set_up(params_grads)

        # step2: split_program
        self._split_program(main_block)

        # step3: add broadcast and reduce ops
        self._add_broadcast_allreduce(main_block)
        main_block._sync_with_cpp()
        startup_block._sync_with_cpp()

        # step4: insert reduce_sum for grad
S
update  
sandyhouse 已提交
169 170 171 172 173 174 175
        # grad_scale_coeff = self.role_maker._worker_num()
        # if self._as_outer_parallelism:
        #     grad_scale_coeff = grad_scale_coeff / self._inner_parallelism_size
        # insert_scale_loss_grad_ops(main_block, scale=1.0 / grad_scale_coeff)
        sharding_group_size = self.user_defined_strategy.sharding_configs[
            'sharding_group_size']
        insert_scale_loss_grad_ops(main_block, scale=1.0 / sharding_group_size)
176 177 178 179 180
        main_block._sync_with_cpp()

        # step5: remove unneeded ops and vars from block
        self._prune_main_program(main_block)
        self._prune_startup_program(startup_block)
S
update  
sandyhouse 已提交
181 182 183 184
        if self.hybrid_dp:
            self._initialization_broadcast(startup_program)

        if self.use_pipeline:
R
update  
root 已提交
185
            # pp_optimizer._rename_gradient_var_name(main_block)
S
update  
sandyhouse 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            # crop ops
            for idx, op in reversed(list(enumerate(main_block.ops))):
                # if op.type == 'fill_constant' and int(op.attr('op_role')) == 16:
                #     out_name = op.output_arg_names[0]
                #     if not 'GRAD' in out_name: continue
                #     param_name = out_name.strip("@GRAD")
                #     #if main_block.has_var(out_name): continue
                #     if self._shard.has_param(param_name): continue
                #     main_block._remove_op(idx)
                if is_update_op(op):
                    op_role_var = op.attr('op_role_var')
                    param_name = op_role_var[0]
                    if not self._shard.has_param(param_name):
                        main_block._remove_op(idx)

S
sandyhouse 已提交
201 202 203 204 205 206 207 208 209 210 211
            for idx, op in reversed(list(enumerate(main_block.ops))):
                if op.type != 'cast': continue
                in_name = op.input_arg_names[0]
                if in_name not in self._params: continue
                #if self._shard.has_param(param_name): continue
                if in_name not in main_block.vars:
                    main_block._remove_op(idx)
            #param_list = []
            #for param_name, grad_name in params_grads:
            #    if self._shard.has_param(param_name):
            #        param_list.append(param_name)
S
update  
sandyhouse 已提交
212
            #pp_optimizer._clear_gradients(main_block, param_list) 
R
update  
root 已提交
213
            accumulated_grad_names = pp_optimizer._accumulate_gradients(
J
update  
JZ-LIANG 已提交
214
                main_block,
R
update  
root 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                pp_allreduce_in_optimize=self.pp_allreduce_in_optimize)
            # accumulated_grad_names = sorted(accumulated_grad_names)
            if self.pp_allreduce_in_optimize:
                print("persistable FP32 grad: ")
                print(accumulated_grad_names)
                first_optimize_op_index = get_first_check_finite_and_unscale_op_idx(
                    main_block)
                insert_reduce_ops(
                    main_block,
                    first_optimize_op_index,
                    self.sharding_ring_id,
                    accumulated_grad_names,
                    self._shard,
                    core.op_proto_and_checker_maker.OpRole.Optimize,
                    use_calc_stream=True)
S
update  
sandyhouse 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            #if not self._shard.has_param(param_name): continue
            ##if not main_block.has_var(grad_name): continue
            #assert main_block.has_var(grad_name)
            #grad_var = main_block.vars[grad_name]
            #grad_var.persistable = True
            #main_block._insert_op(
            #    index=0,
            #    type='fill_constant',
            #    inputs={},
            #    outputs={'Out': [grad_var]},
            #    attrs={
            #        'shape': grad_var.shape,
            #        'dtype': grad_var.dtype,
            #        'value': float(0),
            #        #self._op_device_key: device,
            #        # a trick to run this op once per mini-batch
            #        'op_role': core.op_proto_and_checker_maker.OpRole.LRSched,
            #    })

W
update  
WangXi 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
        pass
        #def _create_var(block, ref_var, name):
        #    """
        #    Create a new var for block, which has the same type,
        #    shape and dtype as ref_var, then rename it with the
        #    name `name`.
        #    """
        #    new_var = block.create_var(
        #        name=name,
        #        shape=ref_var.shape,
        #        dtype=ref_var.dtype,
        #        type=ref_var.type,
        #        lod_level=ref_var.lod_level,
        #        persistable=ref_var.persistable,
        #        is_data=ref_var.is_data,
        #        need_check_feed=ref_var.desc.need_check_feed())
        #    new_var.stop_gradient = ref_var.stop_gradient
        #    return new_var
R
update  
root 已提交
267

W
update  
WangXi 已提交
268 269 270 271 272 273
        #def _rename_arg(op, old_name, new_name):
        #    op_desc = op.desc
        #    if isinstance(op_desc, tuple):
        #        op_desc = op_desc[0]
        #    op_desc._rename_input(old_name, new_name)
        #    op_desc._rename_output(old_name, new_name)
R
update  
root 已提交
274

W
update  
WangXi 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 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
        #print("params_grads:", params_grads)
        #for param_name, grad_name in params_grads:
        #    if not self._shard.has_param(param_name): continue
        #    #if not main_block.has_var(grad_name): continue
        #    assert main_block.has_var(grad_name)
        #    use_fp16 = False
        #    fp16_grad_name = param_name + '.cast_fp16@GRAD'
        #    if main_block.has_var(grad_name):
        #        fp16_grad_var = main_block.vars[fp16_grad_name]
        #        use_fp16 = True
        #    grad_var = main_block.vars[grad_name]
        #    if use_fp16:
        #        cast_grad_var_name = paddle.fluid.unique_name.generate(
        #            grad_name)
        #        cast_var = _create_var(main_block, fp16_grad_var,
        #                               cast_grad_var_name)
        #        cast_var.persistable = False
        #        main_block.append_op(
        #            #index=offset + 1,
        #            type='cast',
        #            inputs={'X': grad_var},
        #            outputs={'Out': cast_var},
        #            attrs={
        #                'in_dtype': grad_var.dtype,
        #                'out_dtype': cast_var.dtype,
        #                'op_role':
        #                core.op_proto_and_checker_maker.OpRole.Backward,
        #            })
        #        #offset += 1
        #        main_block.append_op(
        #            #index=offset + 1,
        #            type='sum',
        #            inputs={'X': [fp16_grad_var, cast_var]},
        #            outputs={'Out': fp16_grad_var},
        #            attrs={
        #                'op_role':
        #                core.op_proto_and_checker_maker.OpRole.Backward,
        #                'op_role_var': op_role_var
        #            })
R
update  
root 已提交
314

W
update  
WangXi 已提交
315 316 317 318 319
        # for index, op in reversed(tuple(enumerate(list(main_block.ops)))):
        #     offset = index
        #     if is_backward_op(op) and (
        #             'op_role_var' in op.attr_names):
        #         op_role_var = op.all_attrs()['op_role_var']
R
update  
root 已提交
320

W
update  
WangXi 已提交
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 364 365 366 367 368 369 370 371 372 373
        #         if len(op_role_var) == 0:
        #             continue
        #         assert len(op_role_var) % 2 == 0
        #         offset = index
        #         for i in range(0, len(op_role_var), 2):
        #             grad_name = op_role_var[i + 1]
        #             if not main_block.has_var(grad_name): continue
        #             grad_var = main_block.vars[grad_name]
        #             if not 'cast_fp16' in grad_name:
        #                 new_grad_var_name = paddle.fluid.unique_name.generate(grad_name)
        #                 new_var = _create_var(main_block, grad_var,
        #                                            new_grad_var_name)
        #                 new_var.persistable = False
        #                 _rename_arg(op, grad_name, new_grad_var_name)
        #                 main_block._insert_op(
        #                     index=offset + 1,
        #                     type='sum',
        #                     inputs={'X': [grad_var, new_var]},
        #                     outputs={'Out': grad_var},
        #                     attrs={
        #                         'op_role': core.op_proto_and_checker_maker.OpRole.Backward,
        #                         'op_role_var': op_role_var
        #                     })
        #                 offset += 1
        #             if 'cast_fp16' in grad_name:
        #                 param_name = op_role_var[i]
        #                 fp32_grad_var_name = param_name + "@GRAD"
        #                 fp32_grad_var = main_block.vars[grad_name]
        #                 cast_grad_var_name = paddle.fluid.unique_name.generate(
        #                     fp32_grad_var_name)
        #                 cast_var = _create_var(main_block, grad_var,
        #                                             cast_grad_var_name)
        #                 cast_var.persistable = False
        #                 main_block._insert_op(
        #                     index=offset + 1,
        #                     type='cast',
        #                     inputs={'X': fp32_grad_var},
        #                     outputs={'Out': cast_var},
        #                     attrs={
        #                         'in_dtype': fp32_grad_var.dtype,
        #                         'out_dtype': cast_var.dtype,
        #                         'op_role': core.op_proto_and_checker_maker.OpRole.Backward,
        #                         # self._op_role_var_key: op_role_var
        #                     })
        #                 offset += 1
        #                 main_block._insert_op(
        #                     index=offset + 1,
        #                     type='sum',
        #                     inputs={'X': [grad_var, cast_var]},
        #                     outputs={'Out': grad_var},
        #                     attrs={
        #                         'op_role': core.op_proto_and_checker_maker.OpRole.Backward,
        #                         'op_role_var': op_role_var})
S
update  
sandyhouse 已提交
374 375
        main_block._sync_with_cpp()

W
update  
WangXi 已提交
376 377 378 379
        # TODO(wangxi): add optimize offload
        offload_helper = OffloadHelper()
        offload_helper.offload(main_block, startup_block)

S
update  
sandyhouse 已提交
380 381 382 383 384 385
        with open("start_sharding_%d" % self.role_maker._worker_index(),
                  'w') as f:
            f.writelines(str(startup_block.program))
        with open("main_sharding_%d" % self.role_maker._worker_index(),
                  'w') as f:
            f.writelines(str(main_block.program))
386 387 388

        # check op dependecy
        check_broadcast(main_block)
S
sandyhouse 已提交
389 390
        #check_allreduce_sum(main_block, self._shard, self.sharding_ring_id,
        #                    self.dp_ring_id)
S
update  
sandyhouse 已提交
391
        #check_allreduce_sum(main_block, self._shard, self.dp_ring_id)
392 393 394 395 396
        self._wait()
        return optimize_ops, params_grads

    def _set_up(self, params_grads):
        # step 1: initialize nccl
397 398 399 400
        self.global_word_size = self.role_maker._worker_num()
        self.global_rank = self.role_maker._worker_index()
        self.endpoints = self.role_maker._get_trainer_endpoints()
        self.current_endpoint = self.endpoints[self.global_rank]
401
        self._collective_helper = CollectiveHelper(self.role_maker,
402 403 404
                                                   self._nrings_sharding)
        # config sharding & dp groups
        self._init_comm()
S
update  
sandyhouse 已提交
405

S
sandyhouse 已提交
406
        # global
S
update  
sandyhouse 已提交
407
        if self._as_outer_parallelism:
J
update  
JZ-LIANG 已提交
408 409 410
            print("global_group_endpoints:", self.global_group_endpoints)
            print("global_rank:", self.global_rank)
            print("global_ring_id:", self.global_group_id)
S
update  
sandyhouse 已提交
411 412 413
            self._collective_helper._init_communicator(
                self._startup_program, self.current_endpoint,
                self.global_group_endpoints, self.global_rank,
J
update  
JZ-LIANG 已提交
414
                self.global_group_id, False)
S
update  
sandyhouse 已提交
415 416

        if self._as_outer_parallelism:
J
update  
JZ-LIANG 已提交
417 418 419
            print("mp_group_endpoints:", self.mp_group_endpoints)
            print("mp_rank:", self.mp_rank)
            print("mp_ring_id:", self.mp_group_id)
S
update  
sandyhouse 已提交
420 421 422 423
            self._collective_helper._init_communicator(
                self._startup_program, self.current_endpoint,
                self.mp_group_endpoints, self.mp_rank, self.mp_group_id, False)

424
        # sharding
S
sandyhouse 已提交
425 426 427
        print("sharding_group_endpoints:", self.sharding_group_endpoints)
        print("sharding_rank:", self.sharding_rank)
        print("sharding_ring_id:", self.sharding_ring_id)
428 429 430
        self._collective_helper._init_communicator(
            self._startup_program, self.current_endpoint,
            self.sharding_group_endpoints, self.sharding_rank,
S
update  
sandyhouse 已提交
431
            self.sharding_ring_id, False)
S
update  
sandyhouse 已提交
432

433 434
        # dp
        if self.hybrid_dp:
435
            self._collective_helper._init_communicator(
436
                self._startup_program, self.current_endpoint,
J
update  
JZ-LIANG 已提交
437
                self.dp_group_endpoints, self.dp_rank, self.dp_ring_id, False)
S
update  
sandyhouse 已提交
438 439
        # pp
        if self.use_pipeline:
S
sandyhouse 已提交
440 441 442
            print("pp_group_endpoints:", self.pp_group_endpoints)
            print("pp_rank:", self.pp_rank)
            print("pp_ring_id:", self.pp_ring_id)
S
update  
sandyhouse 已提交
443
            if self.schedule_mode == 0:  # GPipe
S
sandyhouse 已提交
444 445
                self._collective_helper._init_communicator(
                    self._startup_program, self.current_endpoint,
S
update  
sandyhouse 已提交
446 447 448 449 450 451 452 453
                    self.pp_group_endpoints, self.pp_rank, self.pp_ring_id,
                    False)
                self._collective_helper._init_communicator(
                    self._startup_program, self.current_endpoint,
                    self.pp_group_endpoints, self.pp_rank, self.pp_ring_id + 2,
                    False)
            else:
                for pair in self.pipeline_pair:
S
update  
sandyhouse 已提交
454 455 456
                    pair_key = pair[0] * 1000 + pair[1]
                    ring_id = self.pp_ring_map[pair_key]
                    print("pp pair:{}, ring_id: {}".format(pair, ring_id))
S
update  
sandyhouse 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469
                    if self.pp_rank not in pair: continue
                    pp_group_endpoints = [
                        self.pp_group_endpoints[pair[0]],
                        self.pp_group_endpoints[pair[1]],
                    ]
                    if pair[0] < pair[1]:
                        start_ring_id = self.pp_ring_id + pair[1] - pair[0] - 1
                    else:
                        start_ring_id = self.pp_ring_id + 2 + pair[0] - pair[
                            1] - 1
                    pp_rank = 0 if self.pp_rank == pair[0] else 1
                    self._collective_helper._init_communicator(
                        self._startup_program, self.current_endpoint,
S
update  
sandyhouse 已提交
470
                        pp_group_endpoints, pp_rank, ring_id, False, False)
471

472 473 474 475 476
        startup_block = self._startup_program.global_block()
        startup_block._sync_with_cpp()

        # step 2: split params
        self._params = set([x[0].name for x in params_grads])
477 478
        self._shard.setup(params_grads, self.sharding_rank,
                          self.sharding_group_size)
479 480 481 482 483 484

        # step 3: get broadcast vars
        self._broadcast_vars = self._shard.find_broadcast_params(
            self._main_program.global_block())

    def _wait(self, ):
J
update  
JZ-LIANG 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498
        # only the first parallelsm group that init nccl need to be wait. 
        if self._as_outer_parallelism:
            endpoints = self.role_maker._get_trainer_endpoints()
            current_endpoint = endpoints[self.role_maker._worker_index()]
        else:
            endpoints = self.sharding_group_endpoints[:]
            current_endpoint = self.sharding_group_endpoints[self.sharding_rank]

        if self._as_outer_parallelism:
            if self.role_maker._worker_index() == 0:
                self._collective_helper._wait(current_endpoint, endpoints)
        else:
            if self.sharding_rank == 0:
                self._collective_helper._wait(current_endpoint, endpoints)
499

R
update  
root 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    # def _wait(self, ):
    #     # only the first parallelsm group that init nccl need to be wait. 
    #     if self._as_outer_parallelism:
    #         endpoints = self.role_maker._get_trainer_endpoints()
    #     else:
    #         endpoints = self.sharding_group_endpoints[:]
    #     current_endpoint = endpoints[self.role_maker._worker_index()]

    #     if self._as_outer_parallelism:
    #         if self.role_maker._worker_index() == 0:
    #             self._collective_helper._wait(current_endpoint, endpoints)
    #     else:
    #         if self.sharding_rank == 0:
    #             self._collective_helper._wait(current_endpoint, endpoints)

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    def _split_program(self, block):
        for op_idx, op in reversed(list(enumerate(block.ops))):
            if int(op.attr('op_role')) != int(OpRole.Optimize):
                last_backward_op_idx = op_idx + 1
                break
        segment = ProgramSegment(block)
        segment._end_idx = last_backward_op_idx
        for op_idx in reversed(range(last_backward_op_idx)):
            op = block.ops[op_idx]
            assert (int(op.attr('op_role')) != int(OpRole.Optimize))
            if segment._param_mem >= self._fuse_broadcast_MB:
                segment._start_idx = op_idx + 1
                self._segments.insert(0, segment)
                segment = ProgramSegment(block)
                segment._end_idx = op_idx + 1

            # find broadcast vars
            for input_name in op.desc.input_arg_names():
                if input_name not in self._broadcast_vars:
                    continue
                if input_name in segment._param2broadcast:
                    # skip broadcast because it reuse the old broadcast var
                    broadcast_name = segment._param2broadcast[input_name]
                    if input_name != broadcast_name:
                        op._rename_input(input_name, broadcast_name)
                    continue
                if self._shard.has_param(input_name):
                    broadcast_var_name = input_name
                else:
                    broadcast_var_name = unique_name.generate(input_name +
                                                              "@BroadCast")
                    segment._fill_constant_vars.append(broadcast_var_name)
                segment._param2broadcast[input_name] = broadcast_var_name
                segment._broadcast_vars.append((broadcast_var_name,
                                                self._shard.device(input_name)))
                segment._param_mem += get_var_size(
                    self._main_program.global_block().var(input_name))

            # find reduce vars
R
update  
root 已提交
554 555 556 557
            if self.use_pipeline and self.pp_allreduce_in_optimize:
                # place pipeline gradient allreduce in optimize
                pass
            else:
J
update  
JZ-LIANG 已提交
558 559 560 561 562 563 564 565 566 567 568 569
                if is_backward_op(op) and \
                        OP_ROLE_VAR_KEY in op.attr_names:
                    op_role_var = op.all_attrs()[OP_ROLE_VAR_KEY]
                    if len(op_role_var) != 0:
                        assert len(op_role_var) % 2 == 0
                        for i in range(0, len(op_role_var), 2):
                            param, reduced_grad = op_role_var[i], op_role_var[
                                i + 1]
                            segment._allreduce_vars.append(reduced_grad)
                            #assert (
                            #    reduced_grad not in self._reduced_grads_to_param)
                            self._reduced_grads_to_param[reduced_grad] = param
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

            # find cast op
            if FP16Utils.is_fp16_cast_op(block, op, self._params):
                fp32_param = op.desc.input_arg_names()[0]
                fp16_param = op.desc.output_arg_names()[0]
                if self._shard.has_param(fp32_param):
                    segment._cast_ops[fp16_param] = fp32_param

        if segment._param_mem > 0:
            segment._start_idx = 0
            self._segments.insert(0, segment)
        return

    def _prune_main_program(self, block):
        """
        calculate deps from allredce op to optimize op,
        remove ops and vars not needed in this worker
587 588 589 590 591 592

        1. prune regularization (weight decay)
        2. prune cast_fp32_to_fp16; update amp_infine_checking
        3. prune gradient_clip related; update global_norm_sum
        4. prune optimizer op + param + gradient
            
593 594 595
        """
        weightdecay_helper = WeightDecayHelper()
        weightdecay_helper.prune_weight_decay(block, self._shard)
S
update  
sandyhouse 已提交
596 597 598 599
        # NOTE (JZ-LIANG) the sync of FoundInfinite should among one entire Model Parallelism
        # group. and each Data Parallelism group should have its own sync of FoundInfinite
        Model_Paramllelism_ring_id = self.sharding_ring_id
        if self._as_outer_parallelism:
S
update  
sandyhouse 已提交
600
            Model_Paramllelism_ring_id = self.global_group_id
601
        FP16Utils.prune_fp16(block, self._shard, self._reduced_grads_to_param,
S
update  
sandyhouse 已提交
602 603
                             Model_Paramllelism_ring_id)
        gradientclip_helper = GradientClipHelper(Model_Paramllelism_ring_id)
604 605 606 607 608 609 610 611 612 613 614 615
        gradientclip_helper.prune_gradient_clip(block, self._shard)

        # build prog deps
        reduced_grads = []
        for idx, op in enumerate(block.ops):
            input_names = op.desc.input_arg_names()
            output_names = op.desc.output_arg_names()
            if op.type == "c_allreduce_sum":
                assert (len(output_names) == 1)
                output_name = output_names[0]
                reduced_grads.append(output_name)

616
        # prune optimizer state and param
617 618 619 620 621 622 623 624 625 626 627 628 629 630
        pruned_opti_vars = []
        for var_name in list(block.vars.keys()):
            if self._shard.is_opti_var(var_name) and \
              not self._shard.has_opt_var(var_name):
                pruned_opti_vars.append(var_name)
        program_deps = ProgramDeps(block, reduced_grads, pruned_opti_vars)

        # Init
        for var_name in program_deps._end_vars:
            program_deps._should_removed_var.add(var_name)

        # Prune
        for idx, op in reversed(list(enumerate(block.ops))):
            if op.type in [
S
update  
sandyhouse 已提交
631 632 633 634 635 636 637
                    "c_allreduce_sum",
                    "c_sync_comm_stream",
                    "c_calc_comm_stream",
                    "c_gen_nccl_id",
                    "c_comm_init",
                    'send_v2',
                    'recv_v2',
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
            ]:
                pass
            elif op.type == "conditional_block":
                assert (op.desc.has_attr("sub_block"))
                subblock_idx = op.desc.attr("sub_block").id
                subblock_deps = program_deps.get_sub_block_deps(subblock_idx)
                # only prune amp subblock
                if subblock_deps is None or not self._is_amp_subblock(op):
                    continue
                # init
                reversed_output_vars = []
                for output_name in op.desc.output("Out"):
                    if output_name in program_deps._should_removed_var:
                        subblock_deps._should_removed_var.add(output_name)
                        program_deps.crop_output_var_from_op(idx, output_name)
                    else:
                        reversed_output_vars.append(output_name)
                # prune
                for sub_op_idx, _ in reversed(
                        list(enumerate(subblock_deps._block.ops))):
                    if subblock_deps.should_remove_op(sub_op_idx):
                        subblock_deps.remove_op(sub_op_idx)
                reversed_input_vars = []
                for input_name in op.desc.input('Input'):
                    if input_name not in subblock_deps._should_removed_var:
                        reversed_input_vars.append(input_name)
                    else:
                        program_deps.crop_input_var_from_op(idx, input_name)
                op.desc.set_input('Input', reversed_input_vars)
                op.desc.set_output('Out', reversed_output_vars)
            else:
669 670
                # if all outputs of this op are in _should_removed_var
                # _should_removed_var: opt state not cur shard
671 672 673 674
                if program_deps.should_remove_op(idx):
                    program_deps.remove_op(idx)

        block._sync_with_cpp()
S
update  
sandyhouse 已提交
675 676 677 678 679 680 681 682
        for idx, op in reversed(list(enumerate(block.ops))):
            if op.type == 'concat' and is_optimizer_op(op):
                # remove inputs that not on this card
                reserved_x = []
                for var_name in op.desc.input("X"):
                    if block.has_var(var_name): reserved_x.append(var_name)
                op.desc.set_input('X', reserved_x)
        block._sync_with_cpp()
683 684 685 686 687
        return

    def _add_broadcast_allreduce(self, block):
        """
        _add_broadcast_allreduce
J
update  
JZ-LIANG 已提交
688 689 690

        if combined with pipeline(grad accumulate), 
        the grad allreduce should be done in optimize role
691 692 693
        """
        if len(self._segments) < 1:
            return
694
        # sharding
R
update  
root 已提交
695
        if self.use_pipeline and self.pp_allreduce_in_optimize:
J
update  
JZ-LIANG 已提交
696 697 698
            for idx in range(len(self._segments)):
                assert len(self._segments[idx]._allreduce_vars) == 0

699
        if self._segments[-1]._allreduce_vars:
700 701 702 703 704 705 706
            shard_allredue_vars = self._shard.filter_grads(self._segments[-1]
                                                           ._allreduce_vars)
            if self.hybrid_dp and len(shard_allredue_vars) >= 1:
                insert_sync_comm_ops(block, self._segments[-1]._end_idx,
                                     self.dp_ring_id, shard_allredue_vars)
                insert_allreduce_ops(block, self._segments[-1]._end_idx,
                                     self.dp_ring_id, shard_allredue_vars)
707
            insert_sync_comm_ops(block, self._segments[-1]._end_idx,
708
                                 self.sharding_ring_id,
709
                                 self._segments[-1]._allreduce_vars)
R
update  
root 已提交
710 711 712 713 714 715 716 717 718
            # allreduce --> reduce
            insert_reduce_ops(
                block,
                self._segments[-1]._end_idx,
                self.sharding_ring_id,
                self._segments[-1]._allreduce_vars,
                self._shard,
                op_role=OpRole.Backward,
                use_calc_stream=False)
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

        for idx, segment in reversed(list(enumerate(self._segments))):
            allreduce_vars = self._segments[
                idx - 1]._allreduce_vars if idx > 0 else []
            broadcast_vars = self._segments[idx +
                                            1]._broadcast_vars if idx < len(
                                                self._segments) - 1 else []
            fill_constant_vars = self._segments[
                idx + 2]._fill_constant_vars if idx < len(
                    self._segments) - 2 else []
            cast_ops = self._segments[idx + 2]._cast_ops if idx < len(
                self._segments) - 2 else {}

            for op_idx in reversed(range(segment._start_idx, segment._end_idx)):
                op = block.ops[op_idx]
                for input_name in op.desc.input_arg_names():
                    if input_name in segment._param2broadcast and \
                        input_name != segment._param2broadcast[input_name]:
                        op._rename_input(input_name,
                                         segment._param2broadcast[input_name])

            for param_name, broadcast_name in segment._param2broadcast.items():
                if param_name != broadcast_name:
                    block.create_var(
                        name=broadcast_name,
                        shape=self._main_program.global_block().var(
                            param_name).shape,
                        dtype=self._main_program.global_block().var(param_name)
                        .dtype,
                        persistable=False)

            # step1: remove cast ops
            block._sync_with_cpp()
            segment._end_idx += FP16Utils.remove_cast_op(block, self._params,
                                                         segment, 0)

            # step2: add Sync ops
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
            shard_allredue_vars = self._shard.filter_grads(allreduce_vars)
            if self.hybrid_dp and len(shard_allredue_vars) >= 1:
                insert_sync_comm_ops(block, segment._end_idx, self.dp_ring_id,
                                     shard_allredue_vars)

                broad_cast_vars = [x[0] for x in broadcast_vars]
                if len(broad_cast_vars) > 0:
                    insert_sync_comm_ops(block, segment._end_idx,
                                         self.sharding_ring_id, broad_cast_vars)
            else:
                comm_dep_vars = allreduce_vars + [x[0] for x in broadcast_vars]
                if len(comm_dep_vars) > 0:
                    insert_sync_comm_ops(block, segment._end_idx,
                                         self.sharding_ring_id, comm_dep_vars)

771 772 773 774 775 776 777 778 779 780 781 782 783
            calc_dep_vars = fill_constant_vars + [
                k for k, v in cast_ops.items()
            ] + self._segments[idx]._allreduce_vars

            if len(calc_dep_vars) > 0:
                insert_sync_calc_op(block, segment._end_idx,
                                    [calc_dep_vars[-1]])

            # step3: insert `fill_constant` ops 
            insert_fill_constant_ops(block, segment._end_idx,
                                     fill_constant_vars)

            # step4: add `cast` ops     
S
sandyhouse 已提交
784
            print("cast_ops:", cast_ops)
785 786 787
            insert_cast_ops(block, segment._end_idx, cast_ops)

            # step5: add broadcast ops
788 789
            insert_broadcast_ops(block, segment._start_idx,
                                 self.sharding_ring_id, broadcast_vars)
790
            # step6: add all_reduce ops
791 792 793 794 795 796 797
            # dp
            if self.hybrid_dp and len(shard_allredue_vars) >= 1:
                insert_allreduce_ops(block, segment._start_idx, self.dp_ring_id,
                                     shard_allredue_vars)
                insert_sync_comm_ops(block, segment._start_idx,
                                     self.sharding_ring_id, allreduce_vars)
            # sharding
R
update  
root 已提交
798 799 800 801 802 803 804 805 806
            # allreduce --> reduce
            insert_reduce_ops(
                block,
                segment._start_idx,
                self.sharding_ring_id,
                allreduce_vars,
                self._shard,
                op_role=OpRole.Backward,
                use_calc_stream=False)
807 808 809 810

            block._sync_with_cpp()

        if self._segments[0]._broadcast_vars:
811 812 813
            broadcast_vars = [x[0] for x in self._segments[0]._broadcast_vars]
            insert_sync_comm_ops(block, self._segments[0]._start_idx,
                                 self.sharding_ring_id, broadcast_vars)
814
            insert_broadcast_ops(block, self._segments[0]._start_idx,
815
                                 self.sharding_ring_id,
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
                                 self._segments[0]._broadcast_vars)

        fill_constant_vars = []
        for x in self._segments[:2]:
            fill_constant_vars += x._fill_constant_vars

        # Join
        cast_ops = {}
        for x in self._segments[:2]:
            for k, v in x._cast_ops.items():
                cast_ops[k] = v

        calc_deps_vars = fill_constant_vars + [k for k, v in cast_ops.items()]
        if fill_constant_vars or cast_ops:
            insert_sync_calc_op(block, self._segments[0]._start_idx,
                                [calc_deps_vars[-1]])

        if fill_constant_vars:
            insert_fill_constant_ops(block, self._segments[0]._start_idx,
                                     fill_constant_vars)

        if cast_ops:
            insert_cast_ops(block, self._segments[0]._start_idx, cast_ops)

        return

    def _prune_startup_program(self, block):
        for idx, op in reversed(list(enumerate(block.ops))):
            for output_name in op.desc.output_arg_names():
                if self._shard.has_var(output_name):
                    continue
                #TODO why do we remove op, when only one var is removed
                block._remove_op(idx, sync=False)
                break

        for var_name in list(block.vars.keys()):
            if self._shard.has_var(var_name):
                continue
            block._remove_var(var_name, sync=False)
        block._sync_with_cpp()
856 857 858 859

    def _init_comm(self):

        if self.hybrid_dp:
S
update  
sandyhouse 已提交
860
            assert self._as_outer_parallelism == False, "hybrid dp is conflict when using sharding as outer parallelism"
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
            self.sharding_group_size = self.user_defined_strategy.sharding_configs[
                "sharding_group_size"]
            self.sharding_ring_id = 0
            self.sharding_rank = self.global_rank % self.sharding_group_size

            self.dp_group_size = self.global_word_size // self.sharding_group_size
            self.dp_rank = self.global_rank // self.sharding_group_size
            self.dp_ring_id = self.sharding_rank + 1

            self.sharding_group_endpoints = [
                ep for idx, ep in enumerate(self.endpoints)
                if (idx // self.sharding_group_size) == self.dp_rank
            ]
            self.dp_group_endpoints = [
                ep for idx, ep in enumerate(self.endpoints)
                if (idx % self.sharding_group_size) == self.sharding_rank
            ]
J
update  
JZ-LIANG 已提交
878

879 880 881 882 883 884 885 886 887
            assert self.global_word_size > self.sharding_group_size, \
                "global_word_size: {} should be larger than sharding_group_size: {}".format(self.global_word_size, self.sharding_group_size)
            assert self.global_word_size % self.sharding_group_size == 0, \
                "global_word_size: {} should be divisible to the sharding_group_size: {}".format(self.global_word_size, self.sharding_group_size)
            assert self.dp_group_size *  self.sharding_group_size == self.global_word_size, \
                "global_word_size: {} should be equal to the product of sharding_group_size: {} and dp_group_size: {}".format(
                self.global_word_size,
                self.sharding_group_size,
                self.dp_group_size)
S
update  
sandyhouse 已提交
888 889 890 891 892 893 894 895 896 897
            self.pp_ring_id = -1
            self.pp_rank = -1
            self.pp_group_size = None
            self.pp_group_endpoints = None

            # sharding parallelism is the only model parallelism in the current setting
            self.mp_group_id = self.sharding_ring_id
            self.mp_rank = self.sharding_rank
            self.mp_group_size = self.sharding_group_size
            self.mp_group_endpoints = self.sharding_group_endpoints[:]
898 899 900

            logging.info("Using Sharing&DP mode !")
        else:
S
sandyhouse 已提交
901
            if self._as_outer_parallelism and not self.use_pipeline:
S
update  
sandyhouse 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
                self.sharding_ring_id = 1
                assert self.global_word_size > self._inner_parallelism_size, \
                    "global_word_size: {} should be larger than inner_parallelism_size: {}".format(self.global_word_size, self._inner_parallelism_size)
                assert self.global_word_size % self._inner_parallelism_size == 0, \
                    "global_word_size: {} should be divisible to the inner_parallelism_size: {}".format(self.global_word_size, self._inner_parallelism_size)
                self.sharding_rank = self.global_rank // self._inner_parallelism_size
                self.sharding_group_size = self.role_maker._worker_num(
                ) // self._inner_parallelism_size
                _offset = self.global_rank % self._inner_parallelism_size
                self.sharding_group_endpoints = [
                    ep for idx, ep in enumerate(self.endpoints)
                    if idx % self._inner_parallelism_size == _offset
                ]

                # the current entire model parallelism group is the combination of innert & sharding parallelism
                self.mp_group_id = 2
                self.mp_rank = self.global_rank
                self.mp_group_size = self.role_maker._worker_num()
                self.mp_group_endpoints = self.endpoints[:]
                logging.info("Using Sharing as Outer parallelism mode !")

                # print(
                #     "init the nccl comm for megatron paramllelism, this should be done in Megatron Metaoptimizer"
                # )
                # partition_idx = self.global_rank // self._inner_parallelism_size
                # magetron_endpoints = self.endpoints[
                #     partition_idx * self._inner_parallelism_size:partition_idx *
                #     self._inner_parallelism_size + self._inner_parallelism_size]
                # magetron_rank = self.global_rank % self._inner_parallelism_size

                # self._collective_helper._init_communicator(
                #     program=self._startup_program,
                #     current_endpoint=self.current_endpoint,
                #     endpoints=magetron_endpoints,
                #     rank=magetron_rank,
                #     ring_id=0,
                #     wait_port=True)
                # logging.info("megatron group size: {}".format(
                #     self._inner_parallelism_size))
                # logging.info("megatron rank: {}".format(magetron_rank))
                # logging.info("megatron endpoints: {}".format(
                #     magetron_endpoints))
            if self.use_pipeline:
S
sandyhouse 已提交
945 946 947 948 949 950 951
                if self._inner_parallelism_size == 1:
                    self.sharding_ring_id = 0
                    self.sharding_group_size = self.user_defined_strategy.sharding_configs[
                        'sharding_group_size']
                    self.sharding_rank = self.global_rank % self.sharding_group_size
                    assert self.sharding_group_size * self.pipeline_nodes * self._inner_parallelism_size == self.role_maker._worker_num(
                    )
S
update  
sandyhouse 已提交
952
                    self.pp_ring_id = 20
S
sandyhouse 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965
                    self.pp_rank = self.global_rank // (
                        self.sharding_group_size * self._inner_parallelism_size)
                    self.sharding_group_endpoints = [
                        ep for idx, ep in enumerate(self.endpoints)
                        if (idx // self.sharding_group_size) == self.pp_rank
                    ]
                    self.pp_group_size = self.pipeline_nodes
                    self.pp_group_endpoints = [
                        ep for idx, ep in enumerate(self.endpoints)
                        if (idx % self.sharding_group_size
                            ) == self.sharding_rank
                    ]
                else:
S
update  
sandyhouse 已提交
966
                    self.mp_group_id = 0
S
sandyhouse 已提交
967
                    self.sharding_ring_id = 1
S
update  
sandyhouse 已提交
968
                    self.pp_ring_id = 20
S
update  
sandyhouse 已提交
969 970 971 972 973 974 975 976
                    self.mp_rank = self.global_rank % self._inner_parallelism_size
                    self.mp_group = self.global_rank // self._inner_parallelism_size
                    self.mp_group_endpoints = [
                        ep for idx, ep in enumerate(self.endpoints)
                        if idx // self._inner_parallelism_size == self.mp_group
                    ]
                    print("megatron_group_endpoints:", self.mp_group_endpoints)
                    print("megatron_rank:", self.mp_rank)
S
sandyhouse 已提交
977 978 979
                    # self.cards_per_node = 8
                    self.sharding_group_size = self.user_defined_strategy.sharding_configs[
                        'sharding_group_size']
S
update  
sandyhouse 已提交
980 981 982 983 984 985
                    self.sharding_rank = (
                        self.global_rank //
                        self._inner_parallelism_size) % self.sharding_group_size
                    self.sharding_group_id = self.global_rank // (
                        self._inner_parallelism_size * self.sharding_group_size)
                    self.megatron_rank = self.global_rank % self._inner_parallelism_size
S
sandyhouse 已提交
986 987
                    self.sharding_group_endpoints = [
                        ep for idx, ep in enumerate(self.endpoints)
S
update  
sandyhouse 已提交
988 989 990 991
                        if (idx // (self._inner_parallelism_size *
                                    self.sharding_group_size)
                            ) == self.sharding_group_id and idx %
                        self._inner_parallelism_size == self.megatron_rank
S
sandyhouse 已提交
992
                    ]
S
update  
sandyhouse 已提交
993 994
                    print("sharding_endpoint:", self.sharding_group_endpoints)
                    print("sharding_rank:", self.sharding_rank)
S
sandyhouse 已提交
995 996 997
                    assert self.sharding_group_size * self.pipeline_nodes * self._inner_parallelism_size == self.role_maker._worker_num(
                    )
                    self.pp_rank = self.global_rank // (
S
update  
sandyhouse 已提交
998 999
                        self.sharding_group_size *
                        self._inner_parallelism_size) % self.pipeline_nodes
S
sandyhouse 已提交
1000
                    offset = self.sharding_group_size * self._inner_parallelism_size
S
update  
sandyhouse 已提交
1001
                    # TODO: Adjust for dp
S
sandyhouse 已提交
1002 1003 1004 1005 1006 1007 1008
                    idx_with_pp_0 = self.global_rank % (
                        self.sharding_group_size * self._inner_parallelism_size)
                    self.pp_group_endpoints = []
                    for i in range(self.pipeline_nodes):
                        self.pp_group_endpoints.append(self.endpoints[
                            idx_with_pp_0])
                        idx_with_pp_0 += offset
S
update  
sandyhouse 已提交
1009 1010
                    print("pp_group_endpoints:", self.pp_group_endpoints)
                    print("pp_rank:", self.pp_rank)
S
sandyhouse 已提交
1011 1012 1013 1014 1015

                    #self.pp_group_endpoints = [
                    #    ep for idx, ep in enumerate(self.endpoints)
                    #    if (idx % self.sharding_group_size) == self.sharding_rank
                    #]
S
update  
sandyhouse 已提交
1016 1017 1018 1019
                self.global_group_id = 3
                self.global_rank = self.global_rank
                self.global_group_size = self.role_maker._worker_num()
                self.global_group_endpoints = self.endpoints[:]
S
sandyhouse 已提交
1020
                logging.info("Using Sharing as Outer parallelism mode !")
S
update  
sandyhouse 已提交
1021 1022 1023 1024 1025 1026
                self.dp_ring_id = -1
                self.dp_rank = -1
                self.dp_group_size = None
                self.dp_group_endpoints = None

                logging.info("Using Sharing with pipeline !")
S
sandyhouse 已提交
1027 1028 1029 1030 1031
            #else:
            #    self.sharding_ring_id = 0
            #    self.sharding_rank = self.global_rank
            #    self.sharding_group_size = self.role_maker._worker_num()
            #    self.sharding_group_endpoints = self.endpoints
S
update  
sandyhouse 已提交
1032

S
sandyhouse 已提交
1033 1034 1035 1036 1037
            #    # sharding parallelism is the only model parallelism in the current setting
            #    self.mp_group_id = self.sharding_ring_id
            #    self.mp_rank = self.sharding_rank
            #    self.mp_group_size = self.sharding_group_size
            #    self.mp_group_endpoints = self.sharding_group_endpoints[:]
S
update  
sandyhouse 已提交
1038

S
sandyhouse 已提交
1039
            #    logging.info("Using Sharing alone mode !")
S
update  
sandyhouse 已提交
1040 1041 1042 1043 1044 1045

            self.dp_ring_id = -1
            self.dp_rank = -1
            self.dp_group_size = None
            self.dp_group_endpoints = None

S
sandyhouse 已提交
1046 1047 1048 1049 1050 1051 1052 1053
            #self.pp_ring_id = -1
            #self.pp_rank = -1
            #self.pp_group_size = None
            #self.pp_group_endpoints = None
            #self.dp_ring_id = -1
            #self.dp_rank = -1
            #self.dp_group_size = None
            #self.dp_group_endpoints = None
1054 1055 1056

            logging.info("Using Sharing alone mode !")

S
sandyhouse 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
        #logging.info("global word size: {}".format(self.global_word_size))
        #logging.info("global rank: {}".format(self.global_rank))
        #logging.info("sharding group_size: {}".format(self.sharding_group_size))
        #logging.info("sharding rank: {}".format(self.sharding_rank))
        #logging.info("current model parallelism group_size: {}".format(
        #    self.mp_group_size))
        #logging.info("current model parallelism rank: {}".format(self.mp_rank))
        #logging.info("dp group size: {}".format(self.dp_group_size))
        #logging.info("dp rank: {}".format(self.dp_rank))
        #logging.info("current endpoint: {}".format(self.current_endpoint))
        #logging.info("global word endpoints: {}".format(self.endpoints))
        #logging.info("sharding group endpoints: {}".format(
        #    self.sharding_group_endpoints))
        #logging.info("current model parallelism group endpoints: {}".format(
        #    self.mp_group_endpoints))
        #logging.info("dp group endpoints: {}".format(self.dp_group_endpoints))
1073 1074

        return
S
update  
sandyhouse 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

    def _initialization_broadcast(self, startup_prog):
        """
        this funtion is to ensure the initialization between dp group to be 
        identical when hybrid-dp is used.
        """
        block = startup_prog.global_block()
        params = []
        for param in block.iter_parameters():
            params.append(param)
            block.append_op(
                type='c_broadcast',
                inputs={'X': param},
                outputs={'Out': param},
                attrs={
                    'ring_id': self.dp_ring_id,
                    'root': 0,
                    OP_ROLE_KEY: OpRole.Forward
                })
        block.append_op(
            type='c_sync_comm_stream',
            inputs={'X': params},
            outputs={'Out': params},
            attrs={'ring_id': self.dp_ring_id,
                   OP_ROLE_KEY: OpRole.Forward})