2022-03-23 10:03:39 +00:00
|
|
|
from colossalai.core import global_context as gpc
|
2022-01-07 07:08:36 +00:00
|
|
|
from colossalai.registry import GRADIENT_HANDLER
|
2022-03-18 08:38:32 +00:00
|
|
|
from colossalai.utils.moe import get_moe_epsize_param_dict
|
2022-01-07 07:08:36 +00:00
|
|
|
from ._base_gradient_handler import BaseGradientHandler
|
|
|
|
from ...context.parallel_mode import ParallelMode
|
2022-03-18 08:38:32 +00:00
|
|
|
from .utils import bucket_allreduce
|
2022-03-23 10:03:39 +00:00
|
|
|
from colossalai.context.moe_context import MOE_CONTEXT
|
2022-01-07 07:08:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@GRADIENT_HANDLER.register_module
|
|
|
|
class MoeGradientHandler(BaseGradientHandler):
|
|
|
|
"""A helper class to handle all-reduce operations in a data parallel group and
|
2022-01-21 02:44:30 +00:00
|
|
|
moe model parallel. A all-reduce collective communication will be operated in
|
2022-01-07 07:08:36 +00:00
|
|
|
:func:`handle_gradient` among a data parallel group.
|
|
|
|
For better performance, it bucketizes the gradients of all parameters that are
|
|
|
|
the same type to improve the efficiency of communication.
|
|
|
|
"""
|
|
|
|
|
2022-03-31 10:34:11 +00:00
|
|
|
def __init__(self, model, optimizer=None):
|
|
|
|
super().__init__(model, optimizer)
|
|
|
|
|
2022-01-07 07:08:36 +00:00
|
|
|
def handle_gradient(self):
|
|
|
|
"""A method running an all-reduce operation in a data parallel group.
|
|
|
|
Then running an all-reduce operation for all parameters in experts
|
2022-01-21 02:44:30 +00:00
|
|
|
across moe model parallel group
|
2022-01-07 07:08:36 +00:00
|
|
|
"""
|
|
|
|
global_data = gpc.data_parallel_size
|
|
|
|
|
|
|
|
if global_data > 1:
|
2022-03-31 10:34:11 +00:00
|
|
|
epsize_param_dict = get_moe_epsize_param_dict(self._model)
|
2022-01-07 07:08:36 +00:00
|
|
|
|
2022-04-01 01:22:33 +00:00
|
|
|
|
2022-03-31 10:34:11 +00:00
|
|
|
# epsize is 1, indicating the params are replicated among processes in data parallelism
|
|
|
|
# use the ParallelMode.DATA to get data parallel group
|
2022-03-18 08:38:32 +00:00
|
|
|
# reduce gradients for all parameters in data parallelism
|
2022-03-31 10:34:11 +00:00
|
|
|
if 1 in epsize_param_dict:
|
|
|
|
bucket_allreduce(param_list=epsize_param_dict[1], group=gpc.get_group(ParallelMode.DATA))
|
2022-01-07 07:08:36 +00:00
|
|
|
|
2022-03-31 10:34:11 +00:00
|
|
|
for ep_size in epsize_param_dict:
|
2022-03-19 07:36:25 +00:00
|
|
|
if ep_size != 1 and ep_size != MOE_CONTEXT.world_size:
|
2022-03-31 10:34:11 +00:00
|
|
|
bucket_allreduce(param_list=epsize_param_dict[ep_size],
|
2022-03-21 15:19:47 +00:00
|
|
|
group=MOE_CONTEXT.parallel_info_dict[ep_size].dp_group)
|