File size: 10,702 Bytes
e27ab6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from torch import Tensor
import torch.nn as nn
from typing import Callable
from jaxtyping import Bool, Float
from layers import MultiHeadAttention, PositionwiseFeedForward


class ResidualConnection(nn.Module):
    """
    Implements the (Pre-LN) Residual Connection module, which wraps a sublayer
    (like MultiHeadAttention or FFN) with LayerNormalization and Dropout.

    This is the modern "best practice" used in models like GPT-2, which is
    more stable than the original Post-LN design in "Attention Is All You Need".

    Architecture: x = x + Dropout(Sublayer(LayerNorm(x)))
    """

    def __init__(self, d_model: int, dropout: float = 0.1) -> None:
        """
        Initializes the Residual Connection.

        Args:
            d_model (int): The dimension of the model (D).
            dropout (float): Dropout probability to apply to the sublayer output.
        """
        super().__init__()

        self.dropout: nn.Dropout = nn.Dropout(dropout)

        self.norm: nn.LayerNorm = nn.LayerNorm(d_model)

    def forward(
        self,
        x: Float[Tensor, "B T D"],
        sublayer: Callable[[Float[Tensor, "B T D"]], Float[Tensor, "B T D"]],
    ) -> Float[Tensor, "B T D"]:
        """
        Forward pass for the Residual Connection.

        Args:
            x (Tensor): The input tensor from the previous layer.
            sublayer (Callable): The sublayer module (e.g., MHA or FFN)
                                 to apply the connection to.

        Returns:
            Tensor: The output tensor after the residual connection.
        """

        x_normed = self.norm(x)

        sublayer_output = sublayer(x_normed)

        dropout_output = self.dropout(sublayer_output)

        return x + dropout_output


class EncoderLayer(nn.Module):
    """
    Implements one single Encoder Layer (or "Block") of the Transformer Encoder.

    An Encoder Layer consists of two main sublayers:
    1. A Multi-Head Self-Attention mechanism (MHA).
    2. A Position-wise Feed-Forward Network (FFN).

    Each sublayer is wrapped by a ResidualConnection (which includes
    Pre-LayerNormalization and Dropout).

    Architecture:
    x -> Residual_1(x, MHA) -> x'
    x' -> Residual_2(x', FFN) -> output
    """

    def __init__(
        self, d_model: int, n_heads: int, d_ff: int, dropout: float = 0.1
    ) -> None:
        """
        Initializes the Encoder Layer.

        Args:
            d_model (int): The dimension of the model (D).
            n_heads (int): The number of attention heads (H).
            d_ff (int): The inner dimension of the Feed-Forward Network (D_FF).
            dropout (float): The dropout rate for the residual connections.
        """
        super().__init__()

        self.self_attn: MultiHeadAttention = MultiHeadAttention(d_model, n_heads)

        self.feed_forward: PositionwiseFeedForward = PositionwiseFeedForward(
            d_model, d_ff
        )

        self.residual_1: ResidualConnection = ResidualConnection(d_model, dropout)
        self.residual_2: ResidualConnection = ResidualConnection(d_model, dropout)

    def forward(
        self, x: Float[Tensor, "B T D"], src_mask: Bool[Tensor, "B 1 1 T_k"]
    ) -> Float[Tensor, "B T D"]:
        """
        Forward pass for the Encoder Layer.

        Args:
            x (Tensor): Input tensor from the previous layer or embedding.
            src_mask (Tensor): The padding mask for the source sentence.
                               Shape (B, 1, 1, T_k) allows broadcasting
                               to (B, H, T_q, T_k).

        Returns:
            Tensor: The output tensor of the Encoder Layer.
        """
        x = self.residual_1(
            x,
            lambda x_normed: self.self_attn(
                q=x_normed, k=x_normed, v=x_normed, mask=src_mask
            ),
        )

        x = self.residual_2(x, self.feed_forward)

        return x


class Encoder(nn.Module):
    """
    Implements the full Transformer Encoder, which is a stack of N
    identical EncoderLayers.

    This module takes the input embeddings + positional encodings and
    processes them through N layers of self-attention and FFNs.

    (Best Practice: Uses Pre-LN, so a final LayerNorm is applied
    at the *end* of the stack, before passing to the Decoder).
    """

    def __init__(
        self, d_model: int, n_heads: int, d_ff: int, n_layers: int, dropout: float = 0.1
    ) -> None:
        """
        Initializes the Encoder stack.

        Args:
            d_model (int): The dimension of the model (D).
            n_heads (int): The number of attention heads (H).
            d_ff (int): The inner dimension of the Feed-Forward Network (D_FF).
            n_layers (int): The number of EncoderLayer blocks to stack (N).
            dropout (float): The dropout rate for the residual connections.
        """
        super().__init__()

        self.layers: nn.ModuleList = nn.ModuleList(
            [EncoderLayer(d_model, n_heads, d_ff, dropout) for _ in range(n_layers)]
        )

        self.norm: nn.LayerNorm = nn.LayerNorm(d_model)

    def forward(
        self, x: Float[Tensor, "B T D"], src_mask: Bool[Tensor, "B 1 1 T"]
    ) -> Float[Tensor, "B T D"]:
        """
        Forward pass for the entire Encoder stack.

        Args:
            x (Tensor): Input tensor (usually token embeddings + pos encodings).
            src_mask (Tensor): The padding mask for the source sentence.

        Returns:
            Tensor: The output of the final Encoder layer (the "context"
                    or "memory" for the Decoder).
        """

        for layer in self.layers:
            x = layer(x, src_mask)

        x = self.norm(x)

        return x


class DecoderLayer(nn.Module):
    """
    Implements one single Decoder Layer (or "Block") of the Transformer Decoder.

    A Decoder Layer consists of three main sublayers:
    1. A Masked Multi-Head Self-Attention mechanism (MHA).
    2. A Multi-Head Cross-Attention mechanism (MHA).
    3. A Position-wise Feed-Forward Network (FFN).

    Each sublayer is wrapped by a ResidualConnection (Pre-LN and Dropout).

    Architecture:
    x -> Residual_1(x, Masked_MHA) -> x'
    x' -> Residual_2(x', Cross_MHA, enc_output) -> x''
    x'' -> Residual_3(x'', FFN) -> output
    """

    def __init__(
        self, d_model: int, n_heads: int, d_ff: int, dropout: float = 0.1
    ) -> None:
        """
        Initializes the Decoder Layer.

        Args:
            d_model (int): The dimension of the model (D).
            n_heads (int): The number of attention heads (H).
            d_ff (int): The inner dimension of the Feed-Forward Network (D_FF).
            dropout (float): The dropout rate for the residual connections.
        """
        super().__init__()

        self.self_attn: MultiHeadAttention = MultiHeadAttention(d_model, n_heads)

        self.cross_attn: MultiHeadAttention = MultiHeadAttention(d_model, n_heads)

        self.feed_forward: PositionwiseFeedForward = PositionwiseFeedForward(
            d_model, d_ff
        )

        self.residual_1: ResidualConnection = ResidualConnection(d_model, dropout)
        self.residual_2: ResidualConnection = ResidualConnection(d_model, dropout)
        self.residual_3: ResidualConnection = ResidualConnection(d_model, dropout)

    def forward(
        self,
        x: Float[Tensor, "B T_tgt D"],
        enc_output: Float[Tensor, "B T_src D"],
        src_mask: Bool[Tensor, "B 1 1 T_src"],
        tgt_mask: Bool[Tensor, "B 1 1 T_tgt"],
    ) -> Float[Tensor, "B T_tgt D"]:
        """
        Forward pass for the Decoder Layer.

        Args:
            x (Tensor): Input tensor from the previous decoder layer.
            enc_output (Tensor): The output tensor from the Encoder (K, V).
            src_mask (Tensor): The padding mask for the source (Encoder) input.
            tgt_mask (Tensor): The combined look-ahead and padding mask
                               for the target (Decoder) input.

        Returns:
            Tensor: The output tensor of the Decoder Layer.
        """
        x = self.residual_1(
            x,
            lambda x_normed: self.self_attn(
                q=x_normed, k=x_normed, v=x_normed, mask=tgt_mask
            ),
        )

        x = self.residual_2(
            x,
            lambda x_normed: self.cross_attn(
                q=x_normed, k=enc_output, v=enc_output, mask=src_mask
            ),
        )

        x = self.residual_3(x, self.feed_forward)

        return x


class Decoder(nn.Module):
    """
    Implements the full Transformer Decoder, which is a stack of N
    identical DecoderLayers.

    This module takes the target embeddings + positional encodings and
    processes them through N layers of masked self-attention,
    cross-attention, and FFNs.

    (Best Practice: Uses Pre-LN, so a final LayerNorm is applied
    at the *end* of the stack, before passing to the final Generator).
    """

    def __init__(
        self, d_model: int, n_heads: int, d_ff: int, n_layers: int, dropout: float = 0.1
    ) -> None:
        """
        Initializes the Decoder stack.

        Args:
            d_model (int): The dimension of the model (D).
            n_heads (int): The number of attention heads (H).
            d_ff (int): The inner dimension of the Feed-Forward Network (D_FF).
            n_layers (int): The number of DecoderLayer blocks to stack (N).
            dropout (float): The dropout rate for the residual connections.
        """
        super().__init__()

        self.layers: nn.ModuleList = nn.ModuleList(
            [DecoderLayer(d_model, n_heads, d_ff, dropout) for _ in range(n_layers)]
        )

        self.norm: nn.LayerNorm = nn.LayerNorm(d_model)

    def forward(
        self,
        x: Float[Tensor, "B T_tgt D"],
        enc_output: Float[Tensor, "B T_src D"],
        src_mask: Bool[Tensor, "B 1 1 T_src"],
        tgt_mask: Bool[Tensor, "1 1 T_tgt T_tgt"],
    ) -> Float[Tensor, "B T_tgt D"]:
        """
        Forward pass for the entire Decoder stack.

        Args:
            x (Tensor): Input tensor for the target (embeddings + pos enc).
            enc_output (Tensor): The output from the Encoder (K, V for cross-attn).
            src_mask (Tensor): Padding mask for the source (Encoder) sequence.
            tgt_mask (Tensor): Combined mask for the target (Decoder) sequence.

        Returns:
            Tensor: The output of the final Decoder layer, ready for the
                    final projection (Generator).
        """

        for layer in self.layers:
            x = layer(x, enc_output, src_mask, tgt_mask)

        x = self.norm(x)

        return x