forked from karpathy/nanoGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
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 karpathy#205 from djlisbonne/add_moe
Add Mixture of Experts (MoE) support
- Loading branch information
Showing
5 changed files
with
128 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -11,3 +11,5 @@ csv_logs/ | |
# checkpoint directories | ||
out*/ | ||
.aider* | ||
|
||
venv/* |
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
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,54 @@ | ||
import math | ||
import torch | ||
import torch.nn as nn | ||
from torch.nn import functional as F | ||
|
||
class TopKRouter(nn.Module): | ||
""" Conventional Softmax Top_k Gating network (router) NN for MoE layers """ | ||
def __init__(self, config): | ||
super().__init__() | ||
self.top_k = config.moe_top_k | ||
self.moe_router_scheme = config.moe_router_scheme | ||
self.route_linear = nn.Linear(config.n_embd, config.n_experts) | ||
|
||
def forward(self, x): | ||
logits = self.route_linear(x) | ||
|
||
top_k_logits, indices = logits.topk(self.top_k, dim=-1) | ||
zeros = torch.full_like(logits, float('-inf')) | ||
|
||
sparse_logits = zeros.scatter(-1, indices, top_k_logits) | ||
router_output= F.softmax(sparse_logits, dim=-1) | ||
|
||
return router_output, indices | ||
|
||
|
||
class NoisyTopKRouter(nn.Module): | ||
""" Noisy Top_k Gating network (router) NN for MoE layers """ | ||
def __init__(self, config): | ||
super().__init__() | ||
self.top_k = config.moe_top_k | ||
self.moe_router_scheme = config.moe_router_scheme | ||
self.route_linear = nn.Linear(config.n_embd, config.n_experts) | ||
self.noise_linear = nn.Linear(config.n_embd, config.n_experts) | ||
|
||
def forward(self, x): | ||
logits = self.route_linear(x) | ||
|
||
noise_logits = self.noise_linear(x) | ||
noise = torch.randn_like(logits)*F.softplus(noise_logits) | ||
|
||
top_k_noisy_logits = noise_logits + noise | ||
top_k_logits, indices = logits.topk(self.top_k, dim=1) | ||
|
||
zeros = torch.full_like(top_k_noisy_logits, float('-inf')) | ||
sparse_logits = zeros.scatter(-1, indices, top_k_logits) | ||
|
||
router_output = F.softmax(sparse_logits, dim=-1) | ||
|
||
return router_output, indices | ||
|
||
router_dictionary = { | ||
"softmax": TopKRouter, | ||
"noisy_top_k": NoisyTopKRouter, | ||
} |