You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h2><spanstyle="color:Orange">Computing the Reweighted Padded Attention Mask</span><aclass="headerlink" href="#span-style-color-orange-computing-the-reweighted-padded-attention-mask-span" title="Permalink to this heading">#</a></h2>
1102
1102
<p>Lets create some numbers so we can get a better idea of how this works. Let the tokens be <spanclass="math notranslate nohighlight">\(X = [10, 2, \text{<pad>}]\)</span>, so the third token is a padding token. Lets then also pretend, we pass this to our model, and when we go to compute our attention <spanclass="math notranslate nohighlight">\(QK^T\)</span>. The raw output before the Softmax is below:</p>
1103
-
<divclass="amsmath math notranslate nohighlight" id="equation-a230ab2d-044f-4351-929f-ea68f6ce11ea">
1104
-
<spanclass="eqno">(1)<aclass="headerlink" href="#equation-a230ab2d-044f-4351-929f-ea68f6ce11ea" title="Permalink to this equation">#</a></span>\[\begin{equation}
1103
+
<divclass="amsmath math notranslate nohighlight" id="equation-d5241a6d-9f72-48fc-8054-0a38b162b56d">
1104
+
<spanclass="eqno">(1)<aclass="headerlink" href="#equation-d5241a6d-9f72-48fc-8054-0a38b162b56d" title="Permalink to this equation">#</a></span>\[\begin{equation}
<p>If we ignore padding and everything right now, we can compute softmax for row of the matrix above:</p>
1117
-
<divclass="amsmath math notranslate nohighlight" id="equation-2d164eb8-340f-4ce3-bd4c-e2f2d7925d74">
1118
-
<spanclass="eqno">(2)<aclass="headerlink" href="#equation-2d164eb8-340f-4ce3-bd4c-e2f2d7925d74" title="Permalink to this equation">#</a></span>\[\begin{equation}
1117
+
<divclass="amsmath math notranslate nohighlight" id="equation-48203bcc-acc5-41e4-8587-c0a6c10607fb">
1118
+
<spanclass="eqno">(2)<aclass="headerlink" href="#equation-48203bcc-acc5-41e4-8587-c0a6c10607fb" title="Permalink to this equation">#</a></span>\[\begin{equation}
<p>But what we need is to mask out all the tokens in this matrix related to padding. Just like we did in <aclass="reference external" href="https://github.com/priyammaz/HAL-DL-From-Scratch/tree/main/PyTorch%20for%20NLP/GPT">GPT</a>, we will fill in the indexes of the that we want to mask with <spanclass="math notranslate nohighlight">\(-\infty\)</span>. If only the last token was a padding token in our sequence, then the attention before the softmax should be written as:</p>
1137
-
<divclass="amsmath math notranslate nohighlight" id="equation-0253ffa2-35b1-488c-bcf4-d9752f37cee9">
1138
-
<spanclass="eqno">(3)<aclass="headerlink" href="#equation-0253ffa2-35b1-488c-bcf4-d9752f37cee9" title="Permalink to this equation">#</a></span>\[\begin{equation}
1137
+
<divclass="amsmath math notranslate nohighlight" id="equation-579f0937-3f63-4f69-a64b-235de2e41a33">
1138
+
<spanclass="eqno">(3)<aclass="headerlink" href="#equation-579f0937-3f63-4f69-a64b-235de2e41a33" title="Permalink to this equation">#</a></span>\[\begin{equation}
1139
1139
\begin{bmatrix}
1140
1140
7 & -8 & -\infty \\
1141
1141
-3 & 2 & -\infty \\
1142
1142
1 & 6 & -\infty \\
1143
1143
\end{bmatrix}
1144
1144
\end{equation}\]</div>
1145
1145
<p>Taking the softmax of the rows of this matrix then gives:</p>
1146
-
<divclass="amsmath math notranslate nohighlight" id="equation-4bfa0c8b-4c04-4693-ae6d-2206c57b9264">
1147
-
<spanclass="eqno">(4)<aclass="headerlink" href="#equation-4bfa0c8b-4c04-4693-ae6d-2206c57b9264" title="Permalink to this equation">#</a></span>\[\begin{equation}
1146
+
<divclass="amsmath math notranslate nohighlight" id="equation-c2a4a3f2-727f-4823-9678-0d2e45b64251">
1147
+
<spanclass="eqno">(4)<aclass="headerlink" href="#equation-c2a4a3f2-727f-4823-9678-0d2e45b64251" title="Permalink to this equation">#</a></span>\[\begin{equation}
1148
1148
\text{Softmax}
1149
1149
\begin{bmatrix}
1150
1150
7 & -8 & -\infty \\
@@ -1186,8 +1186,8 @@ <h3><span style="color:LightGreen">Repeating to Match Attention Matrix Shape</sp
1186
1186
<p><codeclass="docutils literal notranslate"><spanclass="pre">attn.shape</span></code> - (Batch x seq_len x seq_len)</p>
1187
1187
<p><codeclass="docutils literal notranslate"><spanclass="pre">mask.shape</span></code> - (Batch x seq_len)</p>
1188
1188
<p>It is clear that our mask is missing a dimension, and we need to repeat it. Lets take sequence_1 for instance that has a mask of [True, True, True, False]. Because the sequence length here is 4, lets repeat this row 4 times:</p>
1189
-
<divclass="amsmath math notranslate nohighlight" id="equation-ed607000-2e59-4f14-a321-36f1bd479965">
1190
-
<spanclass="eqno">(5)<aclass="headerlink" href="#equation-ed607000-2e59-4f14-a321-36f1bd479965" title="Permalink to this equation">#</a></span>\[\begin{bmatrix}
1189
+
<divclass="amsmath math notranslate nohighlight" id="equation-a60b1a61-35b1-4942-a0de-f3d82ca3bce0">
1190
+
<spanclass="eqno">(5)<aclass="headerlink" href="#equation-a60b1a61-35b1-4942-a0de-f3d82ca3bce0" title="Permalink to this equation">#</a></span>\[\begin{bmatrix}
1191
1191
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
1192
1192
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
1193
1193
\textrm{True} & \textrm{True} & \textrm{True} & \textrm{False} \\
<h3><spanstyle="color:LightGreen">Computing the Reweighted Causal Attention Mask</span><aclass="headerlink" href="#span-style-color-lightgreen-computing-the-reweighted-causal-attention-mask-span" title="Permalink to this heading">#</a></h3>
1449
1449
<p>Lets pretend the raw outputs of <spanclass="math notranslate nohighlight">\(QK^T\)</span>, before the softmax, is below:</p>
1450
-
<divclass="amsmath math notranslate nohighlight" id="equation-b19cc58c-07a4-493d-9726-af9cfb39ab99">
1451
-
<spanclass="eqno">(6)<aclass="headerlink" href="#equation-b19cc58c-07a4-493d-9726-af9cfb39ab99" title="Permalink to this equation">#</a></span>\[\begin{equation}
1450
+
<divclass="amsmath math notranslate nohighlight" id="equation-8ad62b77-687e-4290-8db7-aaa61c8b985f">
1451
+
<spanclass="eqno">(6)<aclass="headerlink" href="#equation-8ad62b77-687e-4290-8db7-aaa61c8b985f" title="Permalink to this equation">#</a></span>\[\begin{equation}
1452
1452
\begin{bmatrix}
1453
1453
7 & -8 & 6 \\
1454
1454
-3 & 2 & 4 \\
@@ -1459,8 +1459,8 @@ <h3><span style="color:LightGreen">Computing the Reweighted Causal Attention Mas
<p>Then, we can compute softmax for row of the matrix above:</p>
1462
-
<divclass="amsmath math notranslate nohighlight" id="equation-3b9abc65-eeb7-427f-a4aa-5d72ba8cf814">
1463
-
<spanclass="eqno">(7)<aclass="headerlink" href="#equation-3b9abc65-eeb7-427f-a4aa-5d72ba8cf814" title="Permalink to this equation">#</a></span>\[\begin{equation}
1462
+
<divclass="amsmath math notranslate nohighlight" id="equation-9ba80b99-5751-4c89-aeb1-1417f3ffc534">
1463
+
<spanclass="eqno">(7)<aclass="headerlink" href="#equation-9ba80b99-5751-4c89-aeb1-1417f3ffc534" title="Permalink to this equation">#</a></span>\[\begin{equation}
1464
1464
\text{Softmax}
1465
1465
\begin{bmatrix}
1466
1466
7 & -8 & 6 \\
@@ -1499,17 +1499,17 @@ <h3><span style="color:LightGreen">Computing the Reweighted Causal Attention Mas
<p>So we have exactly what we want! The attention weight of the last value is set to 0, so when we are on the second vector <spanclass="math notranslate nohighlight">\(x_2\)</span>, we cannot look forward to the future value vectors <spanclass="math notranslate nohighlight">\(v_3\)</span>, and the remaining parts add up to 1 so its still a probability vector! To do this correctly for the entire matrix, we can just substitute in the top triangle of <spanclass="math notranslate nohighlight">\(QK^T\)</span> with <spanclass="math notranslate nohighlight">\(-\infty\)</span>. This would look like:</p>
1502
-
<divclass="amsmath math notranslate nohighlight" id="equation-760a21b3-e8a1-4aec-b493-d61ffeb78f22">
1503
-
<spanclass="eqno">(8)<aclass="headerlink" href="#equation-760a21b3-e8a1-4aec-b493-d61ffeb78f22" title="Permalink to this equation">#</a></span>\[\begin{equation}
1502
+
<divclass="amsmath math notranslate nohighlight" id="equation-aab30ec1-747d-4c44-9c19-e3a142972185">
1503
+
<spanclass="eqno">(8)<aclass="headerlink" href="#equation-aab30ec1-747d-4c44-9c19-e3a142972185" title="Permalink to this equation">#</a></span>\[\begin{equation}
1504
1504
\begin{bmatrix}
1505
1505
7 & -\infty & -\infty \\
1506
1506
-3 & 2 & -\infty \\
1507
1507
1 & 6 & -2 \\
1508
1508
\end{bmatrix}
1509
1509
\end{equation}\]</div>
1510
1510
<p>Taking the softmax of the rows of this matrix then gives:</p>
1511
-
<divclass="amsmath math notranslate nohighlight" id="equation-629fd4eb-96a4-4077-a5ff-4765234df0f9">
1512
-
<spanclass="eqno">(9)<aclass="headerlink" href="#equation-629fd4eb-96a4-4077-a5ff-4765234df0f9" title="Permalink to this equation">#</a></span>\[\begin{equation}
1511
+
<divclass="amsmath math notranslate nohighlight" id="equation-6e0ab1b6-62e9-4b8d-8a64-7e80ef4002c4">
1512
+
<spanclass="eqno">(9)<aclass="headerlink" href="#equation-6e0ab1b6-62e9-4b8d-8a64-7e80ef4002c4" title="Permalink to this equation">#</a></span>\[\begin{equation}
0 commit comments