-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from xrsrke/feature/moe
[Feature] Add ExpertParallel with Top1 routing
- Loading branch information
Showing
10 changed files
with
256 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
from typing import List | ||
|
||
from torchtyping import TensorType | ||
|
||
|
||
class ExpertContext: | ||
_instance = None | ||
|
||
def __init__(self): | ||
self.aux_loss = [] | ||
self.z_loss = [] | ||
|
||
def push_aux_loss(self, aux_loss: TensorType): | ||
self.aux_loss.append(aux_loss) | ||
|
||
def pop_all_aux_loss(self) -> List[TensorType]: | ||
aux_loss, self.aux_loss = self.aux_loss, [] | ||
return aux_loss | ||
|
||
def push_z_loss(self, z_loss: TensorType): | ||
self.z_loss.append(z_loss) | ||
|
||
def pop_all_z_loss(self) -> List[TensorType]: | ||
z_loss, self.z_loss = self.z_loss, [] | ||
return z_loss | ||
|
||
@classmethod | ||
def get_instance(cls) -> ExpertContext: | ||
if not cls._instance: | ||
cls._instance = ExpertContext() | ||
return cls._instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
from typing import Callable | ||
|
||
import torch | ||
from torchtyping import TensorType | ||
|
||
from pipegoose.nn.expert_parallel.expert_context import ExpertContext | ||
|
||
|
||
class ExpertLoss: | ||
def __init__(self, loss: Callable, aux_weight: float): | ||
self.loss = loss | ||
def __init__(self, loss_func: Callable, aux_weight: float, z_weight: float): | ||
self.loss_func = loss_func | ||
self.aux_weight = aux_weight | ||
self.z_weight = z_weight | ||
|
||
def __call__(self) -> torch.Tensor: | ||
pass | ||
def __call__(self, *args, **kwargs) -> TensorType: | ||
loss = self.loss_func(*args, **kwargs) | ||
expert_context = ExpertContext.get_instance() | ||
loss += self.aux_weight * sum(expert_context.pop_all_aux_loss()) | ||
loss += self.z_weight * sum(expert_context.pop_all_z_loss()) | ||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from pipegoose.nn.expert_parallel.expert_context import ExpertContext | ||
|
||
|
||
def test_expert_context(): | ||
expert_context = ExpertContext.get_instance() | ||
|
||
expert_context.push_aux_loss(1.01) | ||
expert_context.push_z_loss(2.01) | ||
|
||
expert_context.push_aux_loss(1.02) | ||
expert_context.push_z_loss(2.02) | ||
|
||
# make sure that we have a singleton! | ||
expert_context = ExpertContext.get_instance() | ||
|
||
assert expert_context.pop_all_aux_loss() == [1.01, 1.02] | ||
assert expert_context.pop_all_aux_loss() == [] | ||
|
||
assert expert_context.pop_all_z_loss() == [2.01, 2.02] | ||
assert expert_context.pop_all_z_loss() == [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
import torch | ||
from torch import nn | ||
import torch.nn.functional as F | ||
|
||
from pipegoose.nn.expert_parallel import ExpertLoss | ||
from pipegoose.nn.expert_parallel.expert_context import ExpertContext | ||
|
||
|
||
def test_expert_loss(): | ||
loss_func = nn.CrossEntropyLoss() | ||
torch.manual_seed(42) | ||
logits = torch.randn((10, 5)) | ||
gt = torch.randn((10, 5)) | ||
|
||
expert_loss = ExpertLoss(loss_func, aux_weight=0.1) | ||
loss_func = nn.MSELoss() | ||
|
||
expert_loss = ExpertLoss(loss_func, aux_weight=0.1, z_weight=0.2) | ||
expert_context = ExpertContext.get_instance() | ||
|
||
assert expert_loss.aux_weight == 0.1 | ||
assert expert_loss.z_weight == 0.2 | ||
assert expert_loss.loss_func == loss_func | ||
|
||
ExpertLoss.add_aux_loss(1.01) | ||
ExpertLoss.add_z_loss(2.01) | ||
expert_context.push_aux_loss(1.01) | ||
expert_context.push_z_loss(2.01) | ||
|
||
expert_context.push_aux_loss(1.02) | ||
expert_context.push_z_loss(2.02) | ||
|
||
assert expert_loss.get_aux_loss() == [1.01] | ||
assert expert_loss.get_z_loss() == [2.01] | ||
expected_loss = F.mse_loss(logits, gt) + 0.1 * (1.01 + 1.02) + 0.2 * (2.01 + 2.02) | ||
loss = expert_loss(logits, gt) | ||
|
||
ExpertLoss.add_aux_loss(1.02) | ||
ExpertLoss.add_z_loss(2.02) | ||
assert torch.allclose(loss, expected_loss) | ||
|
||
assert expert_loss.get_aux_loss() == [1.01, 1.02] | ||
assert expert_loss.get_z_loss() == [2.01, 2.02] | ||
assert expert_context.aux_loss == [] | ||
assert expert_context.z_loss == [] |
Oops, something went wrong.