File size: 788 Bytes
3b2d368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import torch
import torch.nn as nn
import torch.nn.functional as F

from .attention import Attention
from .mlp import MLP

class TransformerBlock(nn.Module):
    def __init__(self, config):
        super().__init__()
        
        self.config = config
        
        self.attention = Attention(config)
        self.ln_attention = nn.LayerNorm(config.embed_dim, elementwise_affine=True)
        self.dropout_attention = nn.Dropout(0.1)
        
        self.mlp = MLP(config)
        self.ln_mlp = nn.LayerNorm(config.embed_dim, elementwise_affine=True)
        self.dropout_mlp = nn.Dropout(0.1)
        
    def forward(self, x):
        x = x + self.dropout_attention(self.attention(self.ln_attention(x)))
        x = x + self.dropout_mlp(self.mlp(self.ln_mlp(x)))
        return x