MCPcopy Create free account
hub / github.com/BorealisAI/scaleformer / EncoderLayer

Class EncoderLayer

layers/Transformer_EncDec.py:48–71  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46
47
48class EncoderLayer(nn.Module):
49 def __init__(self, attention, d_model, d_ff=None, dropout=0.1, activation="relu"):
50 super(EncoderLayer, self).__init__()
51 d_ff = d_ff or 4 * d_model
52 self.attention = attention
53 self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
54 self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
55 self.norm1 = nn.LayerNorm(d_model)
56 self.norm2 = nn.LayerNorm(d_model)
57 self.dropout = nn.Dropout(dropout)
58 self.activation = F.relu if activation == "relu" else F.gelu
59
60 def forward(self, x, attn_mask=None):
61 new_x, attn = self.attention(
62 x, x, x,
63 attn_mask=attn_mask
64 )
65 x = x + self.dropout(new_x)
66
67 y = x = self.norm1(x)
68 y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
69 y = self.dropout(self.conv2(y).transpose(-1, 1))
70
71 return self.norm2(x + y), attn
72
73
74class Encoder(nn.Module):

Callers 7

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected