Skip to content

Commit

Permalink
[feat] moe: annotate expert params (#140)
Browse files Browse the repository at this point in the history
The expert annotation is used by clip_grads and DDP.
  • Loading branch information
msbaines committed Oct 16, 2020
1 parent d99c445 commit ee88bb1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fairscale/nn/moe/moelayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def __init__(self, gate: Module, expert: Module, group: Optional[Any] = None) ->
self.gate = gate
self.expert = expert
self.group = group if group is not None else dist.group.WORLD
for p in expert.parameters():
p.expert = True # type: ignore

def all_to_all_dispatch(self, dispatch_mask: Tensor, input: Tensor) -> Tensor:
dispatched_input = torch.einsum("gsec,gsm->egcm", dispatch_mask.float(), input)
Expand Down
11 changes: 11 additions & 0 deletions tests/nn/moe/test_moelayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ def test_create(device):
moe = MOELayer(gate, expert).to(device)


@pytest.mark.parametrize("device", devices)
def test_expert_params(device):
model_dim = 8
num_experts = 4
gate = Top2Gate(model_dim, num_experts)
expert = torch.nn.Linear(model_dim, model_dim)
moe = MOELayer(gate, expert).to(device)
for p in expert.parameters():
assert p.expert is True


@pytest.mark.mpi
@pytest.mark.parametrize("device", ["cpu"])
def test_forward(device):
Expand Down

0 comments on commit ee88bb1

Please sign in to comment.