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

Class DecoderLayer

layers/Transformer_EncDec.py:102–133  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

100
101
102class DecoderLayer(nn.Module):
103 def __init__(self, self_attention, cross_attention, d_model, d_ff=None,
104 dropout=0.1, activation="relu"):
105 super(DecoderLayer, self).__init__()
106 d_ff = d_ff or 4 * d_model
107 self.self_attention = self_attention
108 self.cross_attention = cross_attention
109 self.conv1 = nn.Conv1d(in_channels=d_model, out_channels=d_ff, kernel_size=1)
110 self.conv2 = nn.Conv1d(in_channels=d_ff, out_channels=d_model, kernel_size=1)
111 self.norm1 = nn.LayerNorm(d_model)
112 self.norm2 = nn.LayerNorm(d_model)
113 self.norm3 = nn.LayerNorm(d_model)
114 self.dropout = nn.Dropout(dropout)
115 self.activation = F.relu if activation == "relu" else F.gelu
116
117 def forward(self, x, cross, x_mask=None, cross_mask=None):
118 x = x + self.dropout(self.self_attention(
119 x, x, x,
120 attn_mask=x_mask
121 )[0])
122 x = self.norm1(x)
123
124 x = x + self.dropout(self.cross_attention(
125 x, cross, cross,
126 attn_mask=cross_mask
127 )[0])
128
129 y = x = self.norm2(x)
130 y = self.dropout(self.activation(self.conv1(y.transpose(-1, 1))))
131 y = self.dropout(self.conv2(y).transpose(-1, 1))
132
133 return self.norm3(x + y)
134
135
136class Decoder(nn.Module):

Callers 3

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected