raw_program_optimizer.py 19.1 KB
Newer Older
1
#   Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# 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

from __future__ import print_function
from __future__ import division
import os
17 18
import collections
import numpy as np
19 20 21

import paddle.fluid as fluid
from paddle.fluid import core, unique_name
22
from paddle.fluid.dygraph import Layer, LayerList
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
from ..base.private_helper_function import wait_server_ready
from .meta_optimizer_base import MetaOptimizerBase
from .common import OpRole, OP_ROLE_KEY, OP_ROLE_VAR_KEY, CollectiveHelper, is_loss_grad_op, is_backward_op, is_optimizer_op


class RawProgramOptimizer(MetaOptimizerBase):
    def __init__(self, optimizer):
        super(RawProgramOptimizer, self).__init__(optimizer)
        self.inner_opt = optimizer
        self.meta_optimizers_white_list = [
            "RecomputeOptimizer",
            "AMPOptimizer",
        ]
        self.meta_optimizers_black_list = ["GraphExecutionOptimizer", ]
        self.global_ring_id = 0

    def _set_basic_info(self, loss, role_maker, user_defined_optimizer,
                        user_defined_strategy):
        super(RawProgramOptimizer, self)._set_basic_info(
            loss, role_maker, user_defined_optimizer, user_defined_strategy)
        self.without_graph_optimization = user_defined_strategy.without_graph_optimization
44 45 46
        self.fuse_all_reduce_ops = user_defined_strategy.fuse_all_reduce_ops
        if self.fuse_all_reduce_ops:
            self.fuse_grad_size_in_num = user_defined_strategy.fuse_grad_size_in_num
47
            self.calc_comm_same_stream = user_defined_strategy._calc_comm_same_stream
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    def _can_apply(self):
        if not self.role_maker._is_collective:
            return False

        if self.without_graph_optimization == True:
            return True
        return False

    def _disable_strategy(self, dist_strategy):
        dist_strategy.without_graph_optimization = False

    def _enable_strategy(self, dist_strategy, context):
        dist_strategy.without_graph_optimization = True

    def _broadcast_params(self, ring_id):
        block = self.startup_program.global_block()
        param = None
        for param in block.iter_parameters():
            if param.is_distributed:
                continue

            block.append_op(
                type='c_broadcast',
                inputs={'X': param},
                outputs={'Out': param},
                attrs={
                    'ring_id': ring_id,
                    'root': 0,
                    OP_ROLE_KEY: OpRole.Forward
                })

        if not param: return  # no parameter on this device
        block.append_op(
            type='c_sync_comm_stream',
            inputs={'X': param},
            outputs={'Out': param},
            attrs={'ring_id': ring_id,
                   OP_ROLE_KEY: OpRole.Forward})

    def _get_process_group_info(self):
        # global ring info
        self.global_endpoints = self.endpoints
        self.global_rank = self.rank
        self.global_nranks = self.nranks

    def _init_process_group(self):
        self._get_process_group_info()
        collective_helper = CollectiveHelper(self.role_maker, wait_port=False)
        # Create global ring for all gpus (ring_id = 0)
        collective_helper._init_communicator(
            self.startup_program, self.current_endpoint, self.global_endpoints,
            self.global_rank, self.global_ring_id, True, self.global_ring_id,
            True)
        self._broadcast_params(self.global_ring_id)

    def minimize_impl(self,
                      loss,
                      startup_program=None,
                      parameter_list=None,
                      no_grad_set=None):
        self.endpoints = self.role_maker._get_trainer_endpoints()
        self.current_endpoint = self.endpoints[self.role_maker._worker_index()]
        self.rank = self.role_maker._worker_index()
        self.nranks = self.role_maker._worker_num()
        if startup_program is None:
            startup_program = fluid.default_startup_program()
        self.startup_program = startup_program

        block = loss.block
        program = block.program
        self.main_program = program

        optimize_ops, params_grads = self.inner_opt.minimize(
            loss, startup_program, parameter_list, no_grad_set)
李季 已提交
123 124
        if self.nranks == 1:
            return optimize_ops, params_grads
125 126 127 128 129 130 131 132 133
        self._init_process_group()

        self.main_program = program
        if self.nranks > 1:
            self._transpile_main_program(loss)
        return optimize_ops, params_grads

    def _transpile_main_program(self, loss):
        self._insert_loss_grad_ops(loss)
134
        if self.fuse_all_reduce_ops:
135 136 137
            self._allreduce_fusion_program()
        else:
            self._insert_allreduce_ops()
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    def _insert_loss_grad_ops(self, loss):
        """
        In order to keep the learning rate consistent in different numbers of
        training workers, we scale the loss grad by the number of workers
        """
        block = self.main_program.global_block()
        for idx, op in reversed(list(enumerate(block.ops))):
            if is_loss_grad_op(op):
                loss_grad_var = block.vars[op.output_arg_names[0]]
                block._insert_op(
                    idx + 1,
                    type='scale',
                    inputs={'X': loss_grad_var},
                    outputs={'Out': loss_grad_var},
                    attrs={
                        'scale': 1.0 / self.nranks,
                        OP_ROLE_KEY: OpRole.Backward
                    })

    def _insert_allreduce_ops(self):
        block = self.main_program.global_block()
        ring_id = self.global_ring_id
        grad = None
        for idx, op in reversed(list(enumerate(block.ops))):
            if is_backward_op(op) and \
                    OP_ROLE_VAR_KEY in op.attr_names:
                op_role_var = op.attr(OP_ROLE_VAR_KEY)
                if len(op_role_var) == 0:
                    continue
                assert len(op_role_var) % 2 == 0
                offset = 1
                for i in range(0, len(op_role_var), 2):
                    param_name = op_role_var[i]
                    param = block.var(param_name)
                    grad_name = op_role_var[i + 1]
                    grad = block.var(grad_name)
                    if param.is_distributed:
                        continue

                    block._insert_op(
                        idx + offset,
                        type='c_sync_calc_stream',
                        inputs={'X': grad},
                        outputs={'Out': grad},
                        attrs={OP_ROLE_KEY: OpRole.Backward, })
                    offset += 1
                    block._insert_op(
                        idx + offset,
                        type='c_allreduce_sum',
                        inputs={'X': grad},
                        outputs={'Out': grad},
                        attrs={
                            'ring_id': ring_id,
                            OP_ROLE_KEY: OpRole.Backward
                        })

        if grad is None:
            return

        for idx, op in enumerate(block.ops):
            if is_optimizer_op(op):
                block._insert_op(
                    idx,
                    type='c_sync_comm_stream',
                    inputs={'X': grad},
                    outputs={'Out': grad},
                    attrs={'ring_id': ring_id,
                           OP_ROLE_KEY: OpRole.Backward})
                break
208

209 210 211 212 213 214 215
    # This function helps reduce the number of allreduce by integrating op, which can save communication time.
    # to use allreduce fuse, follow these codes:
    # strategy = paddle.distributed.fleet.DistributedStrategy()
    # strategy.without_graph_optimization = True
    # strategy.fuse_all_reduce_ops = True
    # strategy.calc_comm_same_stream = False
    # strategy.fuse_grad_size_in_num = 8
216 217 218 219
    def _allreduce_fusion_program(self):
        block = self.main_program.global_block()
        ring_id = self.global_ring_id
        record_idx, allreduce_input_vars, allreduce_output_vars = [], [], []
220
        ops = list(enumerate(block.ops))
221

222 223
        for idx, op in reversed(ops):
            # we travers the ops reversely
224 225 226 227 228
            if is_backward_op(op) and \
                    OP_ROLE_VAR_KEY in op.attr_names:
                op_role_var = op.attr(OP_ROLE_VAR_KEY)
                if len(op_role_var) == 0:
                    continue
229 230
                assert len(op_role_var) % 2 == 0, "vars need to be one param var followed by one grad var, " \
                                                  "but got odd number of vars"
231
                for i in range(0, len(op_role_var), 2):
232
                    # handle vars in each op, each time handle a param and a grad
233 234 235 236 237 238 239
                    param_name = op_role_var[i]
                    param = block.var(param_name)
                    grad_name = op_role_var[i + 1]
                    grad = block.var(grad_name)
                    if param.is_distributed:
                        continue
                    if ".cast_fp16@GRAD" in grad_name:
240
                        # when amp=True get the fp16 param
241 242 243 244 245 246 247
                        param_name = param_name + ".cast_fp16"
                        if not block.has_var(param_name):
                            raise ValueError("op cast name error {}".format(
                                op.type))
                        else:
                            param = block.var(param_name)

248 249 250 251
                    if len(allreduce_output_vars) == 0 or \
                            len(allreduce_output_vars[-1]) == \
                            self.fuse_grad_size_in_num:
                        # start of the fusion or last group meets the config size
252 253
                        allreduce_output_vars.append([grad])
                        allreduce_input_vars.append([param])
254 255
                        # add the start and end idx to the record idx
                        record_idx.append([idx, idx])
256
                    else:
257 258 259 260 261
                        # Current group's size is below the config size
                        # append grad and param to the last group (current group)
                        # update the start idx to current op's idx
                        # Since we travers the ops reversely, the idx is descending
                        # we update the first entry of each entry for record_idx
262 263 264 265 266 267 268 269 270
                        allreduce_output_vars[-1].append(grad)
                        allreduce_input_vars[-1].append(param)
                        record_idx[-1][0] = idx

        assert len(allreduce_output_vars) == len(
            record_idx
        ), "It has different lens between the allreduce_output_vars and record_idx."

        if not allreduce_output_vars or not allreduce_input_vars:
271
            # nothing needs to be allreduced
272 273 274
            return

        self.vars = collections.OrderedDict()
275
        index, pos, offset = 0, 0, 0
276
        start, end = record_idx[index]
277
        for idx, op in reversed(ops):
278 279 280
            if idx == start:
                pos = 0
                done_output_vars, done_input_vars = self._split_fuction(
281 282 283
                    allreduce_output_vars[index],  # grad
                    allreduce_input_vars[index]  # param
                )
284
                for id_, done_output_var in enumerate(done_output_vars):
285 286 287 288 289 290 291 292
                    tmp_var = block.create_var(
                        name=unique_name.generate('FusedOutput_{}'.format(
                            done_output_var[0].name)),
                        dtype=done_output_var[0].dtype,
                        persistable=False,
                        stop_gradient=True)
                    self.vars['FusedOutput_{}'.format(done_output_var[0]
                                                      .name)] = tmp_var
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
                    block._insert_op(
                        idx + id_,
                        type="coalesce_tensor",
                        inputs={"Input": done_input_vars[id_]},
                        outputs={
                            "Output": done_output_var,
                            "FusedOutput": tmp_var
                        },
                        attrs={
                            "copy_data": False,
                            "use_align": True,
                            "dtype": done_output_var[0].dtype,
                            OP_ROLE_KEY: OpRole.Backward
                        })
                    pos += 1
309 310

                for id_ in range(len(done_output_vars)):
311 312 313 314 315 316 317
                    x = self.vars['FusedOutput_{}'.format(done_output_vars[id_][
                        0].name)]
                    out = x

                    # NOTE: there still some optimize space if use EVENT instead of sync
                    if not self.calc_comm_same_stream:
                        # need sync if the calc and comm stream are not the same
318 319
                        block._insert_op(
                            end + id_ + pos + 1,
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                            type='c_sync_calc_stream',
                            inputs={'X': x},
                            outputs={'Out': out},
                            attrs={OP_ROLE_KEY: OpRole.Backward})

                    block._insert_op(
                        end + id_ + pos + 1
                        if self.calc_comm_same_stream else end + id_ + pos + 2,
                        type='c_allreduce_sum',
                        inputs={'X': x},
                        outputs={'Out': out},
                        attrs={
                            'ring_id': ring_id,
                            'use_calc_stream': self.calc_comm_same_stream,
                            OP_ROLE_KEY: OpRole.Backward
                        })

337 338
                index += 1
                if len(record_idx) == index:
339
                    break
340 341
                start, end = record_idx[index]

342 343
        if not self.calc_comm_same_stream:
            # need sync if the calc and comm stream are not the same
344 345 346 347 348 349 350 351 352 353 354 355 356
            for idx, op in enumerate(block.ops):
                if is_optimizer_op(op):
                    block._insert_op(
                        idx,
                        type='c_sync_comm_stream',
                        inputs={'X': block.create_var()},
                        outputs={'Out': block.create_var()},
                        attrs={
                            'ring_id': ring_id,
                            OP_ROLE_KEY: OpRole.Backward
                        })
                    break

357 358
    # Integrate grads of the same type to form a combination.
    # If combination is selected, will return grads of the same type in a groups.
359 360 361 362
    # For example:[(fp16, fp16), (fp32), (fp16)] -> [(fp16, fp16, fp16), (fp32)]
    def _split_fuction(self,
                       allreduce_output_vars,
                       allreduce_input_vars,
363
                       combination=True):
364
        input_vars, final_input_vars, output_vars, final_output_vars = [], [], [], []
365 366
        if len(allreduce_output_vars) == 1:
            # only have one var to handle
367 368 369 370 371
            final_output_vars.append(allreduce_output_vars)
            final_input_vars.append(allreduce_input_vars)
            return final_output_vars, final_input_vars

        for idx in range(len(allreduce_input_vars) - 1):
372
            # the last var needs to be handled differently
373 374
            if allreduce_input_vars[idx].dtype == allreduce_input_vars[idx +
                                                                       1].dtype:
375 376
                # if current var and next var are in same type
                # append current var to input_vars
377 378
                input_vars.append(allreduce_input_vars[idx])
                if idx == len(allreduce_input_vars) - 2:
379 380 381
                    # if current var is the second last var
                    # append the last var to input_vars
                    # and update the final_input_vars
382 383 384
                    input_vars.append(allreduce_input_vars[idx + 1])
                    final_input_vars.append(input_vars)
            else:
385 386 387 388
                # the current var and next var are in different types
                # append current var to input_vars
                # update the final_input_vars
                # reset input_vars to receive a new type
389 390 391 392
                input_vars.append(allreduce_input_vars[idx])
                final_input_vars.append(input_vars)
                input_vars = []
                if idx == len(allreduce_input_vars) - 2:
393 394 395
                    # if current var is the second last var
                    # append the last var to a reset input_vars since they are in different types
                    # and update the final_input_vars
396 397 398 399
                    input_vars.append(allreduce_input_vars[idx + 1])
                    final_input_vars.append(input_vars)

        for idx in range(len(allreduce_output_vars) - 1):
400
            # the procedure for the output vars is the same with that for the input vars
401 402 403 404 405 406 407 408 409 410 411 412 413
            if allreduce_output_vars[idx].dtype == allreduce_output_vars[
                    idx + 1].dtype:
                output_vars.append(allreduce_output_vars[idx])
                if idx == len(allreduce_output_vars) - 2:
                    output_vars.append(allreduce_output_vars[idx + 1])
                    final_output_vars.append(output_vars)
            else:
                output_vars.append(allreduce_output_vars[idx])
                final_output_vars.append(output_vars)
                output_vars = []
                if idx == len(allreduce_output_vars) - 2:
                    output_vars.append(allreduce_output_vars[idx + 1])
                    final_output_vars.append(output_vars)
414 415 416 417

        # at this time, all vars in each group in final_input_vars and final_output_vars are in the same type

        if combination:
418 419 420
            input_fp16_vars, input_fp32_vars, output_fp16_vars, output_fp32_vars = [], [], [], []
            for final_input_var in final_input_vars:
                if final_input_var[0].dtype == core.VarDesc.VarType.FP16:
421
                    # extend the group
422 423 424 425 426 427 428 429 430
                    input_fp16_vars.extend(final_input_var)
                else:
                    input_fp32_vars.extend(final_input_var)

            for final_output_var in final_output_vars:
                if final_output_var[0].dtype == core.VarDesc.VarType.FP16:
                    output_fp16_vars.extend(final_output_var)
                else:
                    output_fp32_vars.extend(final_output_var)
431

432 433 434 435 436 437 438 439 440 441 442
            final_output_vars, final_input_vars = [], []
            if output_fp16_vars:
                final_output_vars.append(output_fp16_vars)
            if output_fp32_vars:
                final_output_vars.append(output_fp32_vars)
            if input_fp16_vars:
                final_input_vars.append(input_fp16_vars)
            if input_fp32_vars:
                final_input_vars.append(input_fp32_vars)

        return final_output_vars, final_input_vars