ColossalAI/colossalai/nn/loss/loss_moe.py

51 lines
1.6 KiB
Python
Raw Normal View History

2022-01-07 07:08:36 +00:00
import torch.nn as nn
from colossalai.registry import LOSSES
from torch.nn.modules.loss import _Loss
from colossalai.context.moe_context import MOE_CONTEXT
2022-01-07 07:08:36 +00:00
@LOSSES.register_module
class MoeCrossEntropyLoss(_Loss):
"""torch.nn.CrossEntropyLoss added with auxiliary loss.
2022-01-21 02:44:30 +00:00
:param aux_weight: Weight of auxiliary loss in total loss
:param args: Args in CrossEntropyLoss
:param kwargs: Kwargs in CrossEntropyLoss
:type aux_weight: float, optional
2022-01-07 07:08:36 +00:00
"""
2022-03-19 07:36:25 +00:00
2022-01-07 07:08:36 +00:00
def __init__(self, aux_weight: float = 0.01, *args, **kwargs):
super().__init__()
self.loss = nn.CrossEntropyLoss(*args, **kwargs)
self.aux_weight = aux_weight
def forward(self, *args):
main_loss = self.loss(*args)
2022-03-19 07:36:25 +00:00
aux_loss = MOE_CONTEXT.get_loss()
2022-01-07 07:08:36 +00:00
return main_loss + self.aux_weight * aux_loss
@LOSSES.register_module
class MoeLoss(_Loss):
"""A wrapper class for any loss module to add with auxiliary loss.
2022-01-21 02:44:30 +00:00
:param aux_weight: Weight of auxiliary loss in total loss
:param loss_fn: Loss function
:param args: Args in loss function
:param kwargs: Kwargs in loss function
:type aux_weight: float
:type loss_fn: Callable
2022-01-07 07:08:36 +00:00
"""
2022-03-19 07:36:25 +00:00
2022-01-07 07:08:36 +00:00
def __init__(self, aux_weight: float, loss_fn, *args, **kwargs):
super().__init__()
self.loss_fn = loss_fn(*args, **kwargs)
self.aux_weight = aux_weight
def forward(self, *args, **kwargs):
main_loss = self.loss_fn(*args, **kwargs)
2022-03-19 07:36:25 +00:00
aux_loss = MOE_CONTEXT.get_loss()
2022-01-07 07:08:36 +00:00
return main_loss + self.aux_weight * aux_loss