Created
August 8, 2019 18:44
-
-
Save thomwolf/190ab88516199122561c4de737f12a45 to your computer and use it in GitHub Desktop.
GPT-2 PyTorch block module
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
class Block(nn.Module): | |
def __init__(self, n_ctx, config, scale=False): | |
super(Block, self).__init__() | |
nx = config.n_embd | |
self.ln_1 = LayerNorm(nx, eps=config.layer_norm_epsilon) | |
self.attn = Attention(nx, n_ctx, config, scale) | |
self.ln_2 = LayerNorm(nx, eps=config.layer_norm_epsilon) | |
self.mlp = MLP(4 * nx, config) | |
def forward(self, x): | |
a = self.attn(self.ln_1(x)) | |
x = x + a | |
m = self.mlp(self.ln_2(x)) | |
x = x + m | |
return x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment