python/paddle/distributed/fleet/meta_optimizers/sharding/gradient_clip_helper.py
def prune_gradient_clip(self, block, shard, ring_ids):
        """
        prune gradient_clip related ops for params that not belong to cur shard
        prune: square, reduce_sum, elementwise_mul
        keep: sum, sqrt, elementwise_max, elementwise_div
        """
        deperated_vars = set()
        deperate_op_idx = set()
        reversed_x_paramname = []
        global_norm_sum_op_idx = -1
        for idx, op in enumerate(block.ops):
            if not self._is_gradient_clip_op(op):
                continue
            if op.type == "sum":
                continue
            deperate_op = False
            for input_name in op.desc.input_arg_names():
                if input_name in deperated_vars:
                    deperate_op = True
                # TODO (JZ-LIANG) revise this for uniform mixed parallelism
                if "@MERGED" in input_name:
                    param_name = input_name.strip("@GRAD@MERGED")
                else:
                    param_name = input_name.strip("@GRAD")
                if shard.is_param(param_name) and \
                  not shard.has_param(param_name):
                    deperate_op = True
                elif shard.is_param(param_name):
                    reversed_x_paramname.append(param_name)

            if deperate_op:
                deperate_op_idx.add(idx)
                for output_name in op.desc.output_arg_names():
                    if output_name not in op.desc.input_arg_names():
                        deperated_vars.add(output_name)

        if not deperated_vars:
            # got no gradient_clip op
            return

        for idx, op in reversed(list(enumerate(block.ops))):
            if not self._is_gradient_clip_op(op):
                continue
            if idx in deperate_op_idx:
                block._remove_op(idx, sync=False)
                continue
            reversed_inputs = []
            if op.type == "sum":
                global_norm_sum_op_idx = idx
                for input_name in op.desc.input_arg_names():
                    if input_name not in deperated_vars:
                        reversed_inputs.append(input_name)

                op.desc.set_input("X", reversed_inputs)
                assert (len(op.desc.output_arg_names()) == 1)
                sum_res = op.desc.output_arg_names()[0]

                # allreduce(mp)->allreduce(sharding)->allreduce(pp)
                idx_offset = 1
                for ring_id in ring_ids:
                    if ring_id == -1: continue
                    # this allreduce should not overlap with calc and should be scheduled in calc stream
                    block._insert_op_without_sync(
                        idx + idx_offset,
                        type='c_allreduce_sum',
                        inputs={'X': sum_res},
                        outputs={'Out': sum_res},
                        attrs={
                            'ring_id': ring_id,
                            'op_namescope': "/gradient_clip_model_parallelism",
                            'use_calc_stream': True,
                            OP_ROLE_KEY: OpRole.Optimize,
                        })
                    idx_offset += 1

        # the grad sum here should take the all and only param in the current shard
        to_check_param = set(reversed_x_paramname)
        should_check_param = set(shard.global_params).intersection(set(
            [param for param, worker_idx in shard.global_param2device.items() \
                if worker_idx == shard.worker_idx]))
        assert to_check_param == should_check_param, "amp check_finite_and_unscale \
        checking miss [{}] and got unexpected [{}]".format(
            should_check_param - to_check_param,
            to_check_param - should_check_param)

        for var_name in deperated_vars:
            block._remove_var(var_name, sync=False)
        block._sync_with_cpp()
        return

        prune gradient_clip related ops for params that not belong to cur shard
        prune: square, reduce_sum, elementwise_mul
        keep: sum, sqrt, elementwise_max, elementwise_div
        
prune gradient_clip related ops for params that not belong to cur shard
prune: square, reduce_sum, elementwise_mul
keep: sum, sqrt, elementwise_max, elementwise_div
prune gradient_clip related ops for params that not belong to cur shard prune : square reduce_sum elementwise_mul keep : sum sqrt elementwise_max elementwise_div
def prune_gradient_clip(self, block, shard, ring_ids):
        deperated_vars = set()
        deperate_op_idx = set()
        reversed_x_paramname = []
        global_norm_sum_op_idx = -1
        for idx, op in enumerate(block.ops):
            if not self._is_gradient_clip_op(op):
                continue
            if op.type == "sum":
                continue
            deperate_op = False
            for input_name in op.desc.input_arg_names():
                if input_name in deperated_vars:
                    deperate_op = True
                if "@MERGED" in input_name:
                    param_name = input_name.strip("@GRAD@MERGED")
                else:
                    param_name = input_name.strip("@GRAD")
                if shard.is_param(param_name) and \
                  not shard.has_param(param_name):
                    deperate_op = True
                elif shard.is_param(param_name):
                    reversed_x_paramname.append(param_name)
            if deperate_op:
                deperate_op_idx.add(idx)
                for output_name in op.desc.output_arg_names():
                    if output_name not in op.desc.input_arg_names():
                        deperated_vars.add(output_name)
        if not deperated_vars:
            return
        for idx, op in reversed(list(enumerate(block.ops))):
            if not self._is_gradient_clip_op(op):
                continue
            if idx in deperate_op_idx:
                block._remove_op(idx, sync=False)
                continue
            reversed_inputs = []
            if op.type == "sum":
                global_norm_sum_op_idx = idx
                for input_name in op.desc.input_arg_names():
                    if input_name not in deperated_vars:
                        reversed_inputs.append(input_name)
                op.desc.set_input("X", reversed_inputs)
                assert (len(op.desc.output_arg_names()) == 1)
                sum_res = op.desc.output_arg_names()[0]
                idx_offset = 1
                for ring_id in ring_ids:
                    if ring_id == -1: continue
                    block._insert_op_without_sync(
                        idx + idx_offset,
                        type='c_allreduce_sum',
                        inputs={'X': sum_res},
                        outputs={'Out': sum_res},
                        attrs={
                            'ring_id': ring_id,
                            'op_namescope': "/gradient_clip_model_parallelism",
                            'use_calc_stream': True,
                            OP_ROLE_KEY: OpRole.Optimize,
                        })
                    idx_offset += 1
        to_check_param = set(reversed_x_paramname)
        should_check_param = set(shard.global_params).intersection(set(
            [param for param, worker_idx in shard.global_param2device.items() \
                if worker_idx == shard.worker_idx]))
        assert to_check_param == should_check_param, "amp check_finite_and_unscale \
        checking miss [{}] and got unexpected [{}]".format(
            should_check_param - to_check_param,
            to_check_param - should_check_param)
        for var_name in deperated_vars:
            block._remove_var(var_name, sync=False)
        block._sync_with_cpp()
        return
def prune_gradient_clip ( self , block , shard , ring_ids ) : deperated_vars = set ( ) deperate_op_idx = set ( ) reversed_x_paramname = [ ] global_norm_sum_op_idx = - 1 for idx , op in enumerate ( block . ops ) : if not self . _is_gradient_clip_op ( op ) : continue if op . type == "sum" : continue deperate_op = False for input_name in op . desc . input_arg_names ( ) : if input_name in deperated_vars : deperate_op = True if "@MERGED" in input_name : param_name = input_name . strip ( "@GRAD@MERGED" ) else : param_name = input_name . strip ( "@GRAD" ) if shard . is_param ( param_name ) and not shard . has_param ( param_name ) : deperate_op = True elif shard . is_param ( param_name ) : reversed_x_paramname . append ( param_name ) if deperate_op : deperate_op_idx . add ( idx ) for output_name in op . desc . output_arg_names ( ) : if output_name not in op . desc . input_arg_names ( ) : deperated_vars . add ( output_name ) if not deperated_vars : return for idx , op in reversed ( list ( enumerate ( block . ops ) ) ) : if not self . _is_gradient_clip_op ( op ) : continue if idx in deperate_op_idx : block . _remove_op ( idx , sync = False ) continue reversed_inputs = [ ] if op . type == "sum" : global_norm_sum_op_idx = idx for input_name in op . desc . input_arg_names ( ) : if input_name not in deperated_vars : reversed_inputs . append ( input_name ) op . desc . set_input ( "X" , reversed_inputs ) assert ( len ( op . desc . output_arg_names ( ) ) == 1 ) sum_res = op . desc . output_arg_names ( ) [ 0 ] idx_offset = 1 for ring_id in ring_ids : if ring_id == - 1 : continue block . _insert_op_without_sync ( idx + idx_offset , type = 'c_allreduce_sum' , inputs = { 'X' : sum_res } , outputs = { 'Out' : sum_res } , attrs = { 'ring_id' : ring_id , 'op_namescope' : "/gradient_clip_model_parallelism" , 'use_calc_stream' : True , OP_ROLE_KEY : OpRole . Optimize , } ) idx_offset += 1 to_check_param = set ( reversed_x_paramname ) should_check_param = set ( shard . global_params ) . intersection ( set ( [ param for param , worker_idx in shard . global_param2device . items ( ) if worker_idx == shard . worker_idx ] ) ) assert to_check_param == should_check_param , "amp check_finite_and_unscale \
        checking miss [{}] and got unexpected [{}]" . format ( should_check_param - to_check_param , to_check_param - should_check_param ) for var_name in deperated_vars : block . _remove_var ( var_name , sync = False ) block . _sync_with_cpp ( ) return
prune gradient_clip related ops for params that not belong to cur shard
prune: square, reduce_sum, elementwise_mul
keep: sum, sqrt, elementwise_max, elementwise_div
prune gradient_clip related ops for params that not belong to cur shard prune : square reduce_sum elementwise_mul keep : sum sqrt elementwise_max elementwise_div
"""
        prune gradient_clip related ops for params that not belong to cur shard
        prune: square, reduce_sum, elementwise_mul
        keep: sum, sqrt, elementwise_max, elementwise_div
        """ # TODO (JZ-LIANG) revise this for uniform mixed parallelism # got no gradient_clip op # allreduce(mp)->allreduce(sharding)->allreduce(pp) # this allreduce should not overlap with calc and should be scheduled in calc stream # the grad sum here should take the all and only param in the current shard
self
block
shard
ring_ids