dist_default.py 25.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2021 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

15
from paddle.distributed.fleet.meta_optimizers.common import OP_ROLE_KEY, OpRole
16 17 18 19 20 21 22

from ..cost import (
    _g_op_cost_factory,
    build_comp_costs_from_descs,
    build_comp_desc_from_dist_op,
    build_dp_costs,
)
23
from ..dist_attribute import OperatorDistAttr
24
from ..process_group import new_process_group
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
from ..utils import (
    _get_comm_group,
    _get_corresponding_rank,
    compute_compatible_dim_mapping,
    is_prim_op,
    set_dist_op_desc_original_id,
)
from .common import (
    DistributedOperatorImpl,
    DistributedOperatorImplContainer,
    gradient_synchronization,
    is_parameter_related,
    register_distributed_operator_impl,
    register_distributed_operator_impl_container,
)
40

41
__op_not_need_param_init__ = ["while", "cond"]
42
__op_has_shape_attr__ = ["fill_constant_batch_size_like", "fill_constant"]
43

44

45 46 47 48 49 50 51
def prim_operator_data_parallel_functor(ctx, src_op):
    dist_op_context = ctx.dist_op_context
    main_block = dist_op_context.work_block
    startup_block = dist_op_context.startup_block

    var_name = src_op.output_arg_names[0]
    if var_name in ctx.grads_params:
52 53 54
        assert (
            var_name not in ctx.synced_gradient
        ), "in primtive mode, grad is already {} synced".format(var_name)
55 56 57
        ctx.synced_gradient.add(var_name)
        sync_group = new_process_group(ctx.data_parallel_group)

58 59 60 61 62 63 64 65 66 67
        allreduce_op = main_block.append_op(
            type='c_allreduce_sum',
            inputs={'X': [var_name]},
            outputs={'Out': [var_name]},
            attrs={
                'ring_id': sync_group.id,
                'use_calc_stream': True,
                OP_ROLE_KEY: OpRole.Backward,
            },
        )
68 69 70

        param = ctx.grads_params[var_name]
        startup_block = dist_op_context.startup_block
71 72 73 74 75 76 77 78 79 80 81
        new_op = startup_block.append_op(
            type='c_broadcast',
            inputs={'X': [param]},
            outputs={'Out': [param]},
            attrs={
                'ring_id': sync_group.id,
                'root': 0,
                'use_calc_stream': True,
                OP_ROLE_KEY: OpRole.Forward,
            },
        )
82

Z
zhaoyingli 已提交
83
        grad_var = main_block._var_recursive(var_name)
84
        dims_mapping = ctx.get_tensor_dist_attr_for_program(
85 86
            grad_var
        ).dims_mapping
87 88
        dist_attr = ctx.get_op_dist_attr_for_program(src_op)
        process_mesh = dist_attr.process_mesh
89
        op_attr = OperatorDistAttr()
90 91 92 93 94 95 96 97
        op_attr.process_mesh = process_mesh
        op_attr.set_output_dims_mapping(grad_var.name, dims_mapping)
        op_attr.set_input_dims_mapping(grad_var.name, dims_mapping)
        ctx.set_op_dist_attr_for_program(allreduce_op, op_attr)

    return


98
class DistributedDefault(DistributedOperatorImplContainer):
99
    def __init__(self, op_type):
100
        super().__init__(op_type)
101 102


103
register_distributed_operator_impl_container(DistributedDefault("default"))
104 105


106
# Replicated Default
107 108
class DistributedDefaultImpl0(DistributedOperatorImpl):
    def __init__(self, name):
109
        super().__init__(name)
110 111 112
        self._forward_implemented = True
        self._backward_implemented = True

113 114 115 116 117 118 119 120 121 122 123 124
    def calc_cost(self, op_role, dist_op, ctx, cluster):
        """Calculate the cost by the op role."""
        cost = None
        if int(op_role) == int(OpRole.Backward):
            cost = self.calc_bwd_cost(dist_op, ctx, cluster)
        else:
            cost = self.calc_fwd_cost(dist_op, ctx, cluster)
        assert cost is not None
        return cost

    def calc_fwd_cost(self, dist_op, ctx, cluster):
        # calc comp op cost
125 126 127
        desc_mapping = build_comp_desc_from_dist_op(
            dist_op=dist_op, dist_context=ctx
        )
128
        processes = dist_op.dist_attr.process_mesh.process_ids
129
        op_type = dist_op.serial_op.type
130 131 132
        cost_mapping = build_comp_costs_from_descs(
            _g_op_cost_factory[op_type], ctx, processes, desc_mapping, cluster
        )
133 134 135 136 137 138 139
        res_cost = [cost_mapping]

        return res_cost

    def calc_bwd_cost(self, dist_op, ctx, cluster):
        # calc comp op cost
        res = []
140 141 142
        desc_mapping = build_comp_desc_from_dist_op(
            dist_op=dist_op, dist_context=ctx
        )
143 144
        dist_attr = dist_op.dist_attr
        process_mesh = dist_attr.process_mesh
145
        processes = process_mesh.process_ids
146 147
        backward_op = dist_op.serial_op
        op_type = backward_op.type
148 149 150
        cost_mapping = build_comp_costs_from_descs(
            _g_op_cost_factory[op_type], ctx, processes, desc_mapping, cluster
        )
151 152 153 154 155 156 157
        res.append(cost_mapping)

        main_block = backward_op.block
        need_gradient_allreduce = False
        for input_name in backward_op.desc.input_names():
            for varname in backward_op.desc.input(input_name):
                if "@GRAD" not in varname and not is_parameter_related(
158 159
                    varname, main_block
                ):
160
                    var_dim_mapping = dist_attr.get_input_dims_mapping(varname)
161
                    mesh_shape = process_mesh.shape
162 163 164 165 166 167 168 169 170
                    batch_size_axis = var_dim_mapping[0]
                    if batch_size_axis > -1 and mesh_shape[batch_size_axis] > 1:
                        need_gradient_allreduce = True
                        break

        if need_gradient_allreduce:
            for input_name in backward_op.desc.input_names():
                for varname in backward_op.desc.input(input_name):
                    if "@GRAD" not in varname and is_parameter_related(
171 172
                        varname, main_block
                    ):
173
                        var_dim_mapping = dist_attr.get_input_dims_mapping(
174 175
                            varname
                        )
176
                        mesh_shape = process_mesh.shape
177 178 179 180
                        batch_size_axis = var_dim_mapping[0]
                        parallel_axis = batch_size_axis
                        attrs = {"use_calc_stream": True}
                        var_names = [varname + "@GRAD"]
181 182 183 184 185 186 187 188 189
                        build_dp_costs(
                            res,
                            dist_op,
                            ctx,
                            var_names,
                            attrs,
                            parallel_axis,
                            cluster,
                        )
190 191
        return res

192
    def is_input_compatible(self, dist_op):
193 194
        op_desc = dist_op.serial_op.desc
        op_dist_attr = dist_op.dist_attr
195
        batch_dim_mappings = []
196 197 198 199
        input_names = op_desc.input_names()
        xshape_arg_names = []
        if "XShape" in input_names:
            xshape_arg_names = op_desc.input("XShape")
200 201 202
        for arg_name in op_desc.input_arg_names():
            serial_tensor = dist_op.get_serial_input(arg_name)
            dims_mapping = op_dist_attr.get_input_dims_mapping(arg_name)
203 204 205 206
            if serial_tensor.is_parameter:
                for mapping in dims_mapping:
                    if mapping != -1:
                        return False
207
                continue
208 209 210 211 212
            if arg_name not in xshape_arg_names:
                if len(dims_mapping) > 1:
                    for mapping in dims_mapping[1:]:
                        if mapping != -1:
                            return False
213 214
                if len(dims_mapping) >= 1:
                    batch_dim_mappings.append(dims_mapping[0])
215 216 217 218 219 220 221
            else:
                if dims_mapping[0] != -1:
                    return False
                if len(dims_mapping) > 2:
                    for mapping in dims_mapping[2:]:
                        if mapping != -1:
                            return False
222 223 224 225 226 227
                if len(dims_mapping) >= 2:
                    batch_dim_mappings.append(dims_mapping[1])

        if compute_compatible_dim_mapping(batch_dim_mappings) is None:
            return False

228
        return True
229

230
    def is_output_compatible(self, dist_op):
231 232 233
        op_desc = dist_op.serial_op.desc
        op_dist_attr = dist_op.dist_attr
        output_names = op_desc.output_names()
234
        batch_dim_mappings = []
235 236 237 238 239 240
        xshape_arg_names = []
        if "XShape" in output_names:
            xshape_arg_names = op_desc.output("XShape")
        for arg_name in op_desc.output_arg_names():
            serial_tensor = dist_op.get_serial_output(arg_name)
            dims_mapping = op_dist_attr.get_output_dims_mapping(arg_name)
241 242 243 244
            if serial_tensor.is_parameter:
                for mapping in dims_mapping:
                    if mapping != -1:
                        return False
245
                continue
246 247 248 249 250
            if arg_name not in xshape_arg_names:
                if len(dims_mapping) > 1:
                    for mapping in dims_mapping[1:]:
                        if mapping != -1:
                            return False
251 252
                if len(dims_mapping) >= 1:
                    batch_dim_mappings.append(dims_mapping[0])
253 254 255 256 257 258 259
            else:
                if dims_mapping[0] != -1:
                    return False
                if len(dims_mapping) > 2:
                    for mapping in dims_mapping[2:]:
                        if mapping != -1:
                            return False
260 261 262 263 264 265
                if len(dims_mapping) >= 2:
                    batch_dim_mappings.append(dims_mapping[1])

        if compute_compatible_dim_mapping(batch_dim_mappings) is None:
            return False

266 267 268 269 270 271 272
        return True

    def is_auto_compatible(self, dist_op):
        op_desc = dist_op.serial_op.desc
        op_dist_attr = dist_op.dist_attr
        batch_dim_mappings = []
        # Check input compatibility
273 274 275 276
        input_names = op_desc.input_names()
        xshape_arg_names = []
        if "XShape" in input_names:
            xshape_arg_names = op_desc.input("XShape")
277 278
        for arg_name in op_desc.input_arg_names():
            serial_tensor = dist_op.get_serial_input(arg_name)
279
            dims_mapping = op_dist_attr.get_input_dims_mapping(arg_name)
280
            if serial_tensor is not None and serial_tensor.is_parameter:
281 282 283
                for mapping in dims_mapping:
                    if mapping != -1:
                        return False
284
                continue
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            if arg_name not in xshape_arg_names:
                if len(dims_mapping) > 1:
                    for mapping in dims_mapping[1:]:
                        if mapping != -1:
                            return False
                if len(dims_mapping) >= 1:
                    batch_dim_mappings.append(dims_mapping[0])
            else:
                if dims_mapping[0] != -1:
                    return False
                if len(dims_mapping) > 2:
                    for mapping in dims_mapping[2:]:
                        if mapping != -1:
                            return False
                if len(dims_mapping) >= 2:
                    batch_dim_mappings.append(dims_mapping[1])
301 302 303 304 305 306 307 308

        # Check output compatibility
        output_names = op_desc.output_names()
        xshape_arg_names = []
        if "XShape" in output_names:
            xshape_arg_names = op_desc.output("XShape")
        for arg_name in op_desc.output_arg_names():
            serial_tensor = dist_op.get_serial_output(arg_name)
309
            dims_mapping = op_dist_attr.get_output_dims_mapping(arg_name)
310
            if serial_tensor is not None and serial_tensor.is_parameter:
311 312 313
                for mapping in dims_mapping:
                    if mapping != -1:
                        return False
314 315 316 317 318 319
                continue
            if arg_name not in xshape_arg_names:
                if len(dims_mapping) > 1:
                    for mapping in dims_mapping[1:]:
                        if mapping != -1:
                            return False
320 321
                if len(dims_mapping) >= 1:
                    batch_dim_mappings.append(dims_mapping[0])
322 323 324 325 326 327 328
            else:
                if dims_mapping[0] != -1:
                    return False
                if len(dims_mapping) > 2:
                    for mapping in dims_mapping[2:]:
                        if mapping != -1:
                            return False
329 330
                if len(dims_mapping) >= 2:
                    batch_dim_mappings.append(dims_mapping[1])
331 332

        # Check batch dim mapping compatibility
333 334 335 336
        if not all(
            batch_dim_mappings[0] == dim_mapping
            for dim_mapping in batch_dim_mappings
        ):
337 338 339
            return False

        return True
340

341
    def update_dims_mapping(self, dist_op):
342 343 344
        changed = False
        op_desc = dist_op.serial_op.desc
        op_dist_attr = dist_op.dist_attr
345 346

        if op_desc.type() == "while":
347
            return False
348 349 350 351 352 353

        input_names = op_desc.input_names()
        input_xshape_arg_names = []
        if "XShape" in input_names:
            input_xshape_arg_names = op_desc.input("XShape")

354
        output_names = op_desc.output_names()
355
        output_xshape_arg_names = []
356
        if "XShape" in output_names:
357 358
            output_xshape_arg_names = op_desc.output("XShape")

359 360 361 362 363 364
        batch_dim_mappings = []
        for arg_name in op_desc.input_arg_names():
            serial_tensor = dist_op.get_serial_input(arg_name)
            if serial_tensor.is_parameter:
                continue
            dims_mapping = op_dist_attr.get_input_dims_mapping(arg_name)
365 366 367 368 369
            if arg_name not in input_xshape_arg_names:
                if len(dims_mapping) >= 1:
                    batch_dim_mappings.append(dims_mapping[0])
            else:
                batch_dim_mappings.append(dims_mapping[1])
370
        for arg_name in op_desc.output_arg_names():
371
            if op_desc.type() == 'fill_any_like':
372
                input_tensor = dist_op.get_serial_input(
373 374
                    op_desc.input_arg_names()[0]
                )
375 376
                if input_tensor.is_parameter:
                    continue
377 378 379 380
            serial_tensor = dist_op.get_serial_output(arg_name)
            if serial_tensor.is_parameter:
                continue
            dims_mapping = op_dist_attr.get_output_dims_mapping(arg_name)
381
            if arg_name not in output_xshape_arg_names:
382 383
                if len(dims_mapping) >= 1:
                    batch_dim_mappings.append(dims_mapping[0])
384 385 386
            else:
                batch_dim_mappings.append(dims_mapping[1])

387 388 389
        if not batch_dim_mappings:
            return changed

390
        compatible_dim_mapping = compute_compatible_dim_mapping(
391 392
            batch_dim_mappings
        )
393 394 395
        if compatible_dim_mapping is None:
            return False

396 397 398 399 400
        for arg_name in op_desc.input_arg_names():
            serial_tensor = dist_op.get_serial_input(arg_name)
            if serial_tensor.is_parameter:
                continue
            dims_mapping = op_dist_attr.get_input_dims_mapping(arg_name)
401
            if arg_name not in input_xshape_arg_names:
402 403 404 405
                if (
                    len(dims_mapping) >= 1
                    and compatible_dim_mapping != dims_mapping[0]
                ):
406
                    dims_mapping[0] = compatible_dim_mapping
407
                    op_dist_attr.set_input_dims_mapping(arg_name, dims_mapping)
408 409
                    changed = True
            else:
410 411 412 413
                if (
                    len(dims_mapping) >= 2
                    and compatible_dim_mapping != dims_mapping[1]
                ):
414
                    dims_mapping[1] = compatible_dim_mapping
415
                    op_dist_attr.set_input_dims_mapping(arg_name, dims_mapping)
416
                    changed = True
417
        for arg_name in op_desc.output_arg_names():
418
            if op_desc.type() == 'fill_any_like':
419
                input_tensor = dist_op.get_serial_input(
420 421
                    op_desc.input_arg_names()[0]
                )
422 423
                if input_tensor.is_parameter:
                    continue
424 425
            if op_desc.type() in ["shape", "slice"]:
                continue
426 427 428 429
            serial_tensor = dist_op.get_serial_output(arg_name)
            if serial_tensor.is_parameter:
                continue
            dims_mapping = op_dist_attr.get_output_dims_mapping(arg_name)
430
            if arg_name not in output_xshape_arg_names:
431 432 433 434
                if (
                    len(dims_mapping) >= 1
                    and compatible_dim_mapping != dims_mapping[0]
                ):
435
                    dims_mapping[0] = compatible_dim_mapping
436
                    op_dist_attr.set_output_dims_mapping(arg_name, dims_mapping)
437 438
                    changed = True
            else:
439 440 441 442
                if (
                    len(dims_mapping) >= 2
                    and compatible_dim_mapping != dims_mapping[1]
                ):
443
                    dims_mapping[1] = compatible_dim_mapping
444
                    op_dist_attr.set_output_dims_mapping(arg_name, dims_mapping)
445 446 447
                    changed = True

        return changed
448 449 450

    @staticmethod
    def forward(ctx, *args, **kwargs):
451
        dist_op_context = ctx.dist_op_context
452 453 454 455
        main_block = dist_op_context.work_block
        startup_block = dist_op_context.startup_block
        src_op = dist_op_context.cur_src_op
        rank_id = dist_op_context.rank_id
456

457
        # check validation of inputs / outputs
458 459
        for input_name in src_op.desc.input_names():
            assert input_name in kwargs, "input [{}] is not given".format(
460 461
                input_name
            )
462 463 464 465 466
            assert len(kwargs[input_name]) == len(
                src_op.desc.input(input_name)
            ), "number of tensor for input [{}] is not match".format(input_name)
        for output_name in src_op.desc.output_names():
            assert output_name in kwargs, "input [{}] is not given".format(
467 468
                output_name
            )
469 470 471
            assert len(kwargs[output_name]) == len(
                src_op.desc.output(output_name)
            ), "number of tensor for input [{}] is not match".format(
472 473
                output_name
            )
474 475

        # replicate op in dist program
476 477
        dist_op = main_block.append_op(type='nop')
        dist_op_desc = dist_op.desc
478
        dist_op_desc.copy_from(src_op.desc)
479
        set_dist_op_desc_original_id(dist_op_desc, src_op.desc, ctx)
480 481 482 483
        for input_name in src_op.desc.input_names():
            dist_op_desc.set_input(input_name, kwargs[input_name])
        for output_name in src_op.desc.output_names():
            dist_op_desc.set_output(output_name, kwargs[output_name])
484
        # TODO: should we add a new dist attr for the new op here?
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
        if (
            src_op.has_attr('shape')
            and src_op.attr('shape')
            and src_op.type in __op_has_shape_attr__
        ):
            shape_list = src_op.attr('shape')
            Out_var = main_block._var_recursive(kwargs['Out'][0])
            op_dist_attr = ctx.get_op_dist_attr_for_program(src_op)
            dim_mapping = op_dist_attr.get_output_dims_mapping(Out_var.name)
            process_mesh_shape = op_dist_attr.process_mesh.shape
            assert len(shape_list) == len(dim_mapping)
            # modify target shape
            for idx, axis in enumerate(dim_mapping):
                if axis >= 0:
                    if len(shape_list) > idx:
                        shape_list[idx] = (
                            shape_list[idx] // process_mesh_shape[axis]
                        )
            dist_op_desc._set_attr('shape', shape_list)

506 507
        # data parallel synchronization for primtive operators
        from paddle.incubate.autograd import prim_enabled
508

509 510 511 512
        if prim_enabled():
            assert is_prim_op(src_op)
            prim_operator_data_parallel_functor(ctx, src_op)
            return
513 514

        # param initialization sync
515 516 517
        if src_op.type in __op_not_need_param_init__:
            return

518
        for varname in dist_op_desc.input_arg_names():
519 520 521 522 523
            if (
                startup_block.has_var(varname)
                and startup_block.var(varname).is_parameter
                and varname not in dist_op_context.already_init_sync_vars
            ):
524
                dist_op_context.already_init_sync_vars.add(varname)
525
                param = startup_block.var(varname)
526 527 528
                param_dist_attr = ctx.get_tensor_dist_attr_for_program(param)
                process_mesh = param_dist_attr.process_mesh
                dims_mapping = param_dist_attr.dims_mapping
529 530

                # FIXME (JZ-LIANG) Remove this hack to support any op mesh group for Pipeline Parallelism
531
                if rank_id not in process_mesh.process_ids:
532 533 534
                    rank_id = _get_corresponding_rank(
                        ctx, process_mesh, rank_id
                    )
535

536
                # NOTE all not splited axis should be presented in mesh
537
                for axis, size in enumerate(process_mesh.shape):
538 539 540
                    if size <= 1 or axis in dims_mapping:
                        pass
                    else:
541
                        group_ranks = _get_comm_group(
542 543
                            process_mesh.process_ids,
                            process_mesh.shape,
544 545 546
                            axis,
                            rank_id,
                        )
547 548
                        sync_group = new_process_group(group_ranks)

549 550 551 552 553 554 555 556 557 558 559
                        new_op = startup_block.append_op(
                            type='c_broadcast',
                            inputs={'X': param},
                            outputs={'Out': param},
                            attrs={
                                'ring_id': sync_group.id,
                                'root': 0,
                                'use_calc_stream': True,
                                OP_ROLE_KEY: OpRole.Forward,
                            },
                        )
560 561

                        # set distributed attribute
562
                        op_attr = OperatorDistAttr()
563
                        op_attr.process_mesh = process_mesh
564 565 566
                        op_attr.set_output_dims_mapping(
                            param.name, dims_mapping
                        )
567
                        op_attr.set_input_dims_mapping(param.name, dims_mapping)
568
                        ctx.set_op_dist_attr_for_program(new_op, op_attr)
569 570 571 572 573

    @staticmethod
    def backward(ctx, *args, **kwargs):

        # by now the backward function only insert the gradient allreduce for dist op itself
574
        dist_op_context = ctx.dist_op_context
575 576
        main_block = dist_op_context.work_block
        backward_op = dist_op_context.cur_src_op
577
        dist_attr = ctx.get_op_dist_attr_for_program(backward_op)
578 579 580 581 582
        assert (
            dist_attr is not None
        ), "backward op [{}] don't have dist attribute !".format(
            str(backward_op)
        )
583
        rank_id = dist_op_context.rank_id
584

585 586 587
        # check validation of inputs / outputs
        for input_name in backward_op.desc.input_names():
            assert input_name in kwargs, "input [{}] is not given".format(
588 589
                input_name
            )
590 591 592 593 594
            assert len(kwargs[input_name]) == len(
                backward_op.desc.input(input_name)
            ), "number of tensor for input [{}] is not match".format(input_name)
        for output_name in backward_op.desc.output_names():
            assert output_name in kwargs, "input [{}] is not given".format(
595 596
                output_name
            )
597 598 599
            assert len(kwargs[output_name]) == len(
                backward_op.desc.output(output_name)
            ), "number of tensor for input [{}] is not match".format(
600 601
                output_name
            )
602 603

        # replicate op in dist program
604
        dist_op_desc = main_block.append_op(type='nop').desc
605
        dist_op_desc.copy_from(backward_op.desc)
606 607
        # Refer to the related dist op
        set_dist_op_desc_original_id(dist_op_desc, backward_op.desc, ctx)
608 609 610 611 612
        for input_name in backward_op.desc.input_names():
            dist_op_desc.set_input(input_name, kwargs[input_name])
        for output_name in backward_op.desc.output_names():
            dist_op_desc.set_output(output_name, kwargs[output_name])

613 614
        # data parallel gradient synchronization
        act_grad_names = []
615 616
        for input_name in backward_op.desc.input_names():
            for varname in backward_op.desc.input(input_name):
J
JZ-LIANG 已提交
617
                if "@GRAD" not in varname and not is_parameter_related(
618 619
                    varname, main_block
                ):
620
                    act_grad_names.append(varname)
621

622 623 624 625 626
        out_grad_names = []
        for output_name in backward_op.desc.output_names():
            for varname in backward_op.desc.output(output_name):
                if varname in kwargs["grad_var_to_var"]:
                    fwd_name = kwargs["grad_var_to_var"][varname]
Z
zhaoyingli 已提交
627
                    if not main_block._find_var_recursive(fwd_name):
628 629 630 631
                        continue
                    if is_parameter_related(fwd_name, main_block):
                        out_grad_names.append(varname)

632 633 634
        gradient_synchronization(
            ctx, backward_op, act_grad_names, out_grad_names, rank_id
        )
635 636 637


register_distributed_operator_impl(
638 639
    "default", DistributedDefaultImpl0("replicate_parallel")
)