<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>herambve</title><description>Personal website, systems &amp; AI engineering worklogs, and projects by Heramb Vengurlekar</description><link>https://herambve.github.io/</link><language>en-us</language><item><title>Chasing Copy Heads in a Residual Streams Avenue</title><link>https://herambve.github.io/blog/copy-heads-in-transformers/</link><guid isPermaLink="true">https://herambve.github.io/blog/copy-heads-in-transformers/</guid><description>mechanics behind copy heads across different layers and their importance</description><pubDate>Sun, 16 Nov 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;draft: true&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;WIP&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I’ve been looking into attention mechanics recently, and this is my list of questions I want to answer in relation to copy heads to document my understanding:&lt;/p&gt;
&lt;h3 id=&quot;1-definitions&quot;&gt;1. Definitions&lt;/h3&gt;
&lt;p&gt;copy heads, the OV circuit, frobenius distance, identity matrix, singular value decomposition, etc.&lt;/p&gt;
&lt;h3 id=&quot;2-background-of-all-this&quot;&gt;2. Background of all this&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;how did I reach to this point of thinking about copy heads?&lt;/li&gt;
&lt;li&gt;the essence of &lt;code&gt;W_OV&lt;/code&gt;: why we need it, what it does, what it signifies (identity matrix relevance, wwhether there is transformation or whether the information from the incoming residual stream is merely getting put into the blender, when &lt;code&gt;W_OV&lt;/code&gt; is essential: is it calculated at any point during training or inference?)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;3-importance-of-copy-heads-across-different-layers&quot;&gt;3. Importance of copy heads across different layers&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;how do I know when a head is a copy head? more formalized and comprehensive than the definition&lt;/li&gt;
&lt;li&gt;is a copy head in, say, layer 1, as important as that in, say, layer 12 of a transformer?&lt;/li&gt;
&lt;li&gt;is a copy head even essential at all? why don’t we simply focus on transform heads?&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;4-critical-questions&quot;&gt;4. Critical questions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Do we not pass all the new features that are learned in specific heads (in turn specific layers) to the residual stream?&lt;/li&gt;
&lt;li&gt;Does this not mean the features are always there in the residual stream for, say, head 3 in layer 13 if it wants to look up the a specific feature information from, say, head 6 in layer 1? why specifically require a copy head here? (mostly to do with row specifications ⇒ residual stream is like a spreadsheet of rows and columns ⇒ what if we want the info on row 3 of head 1.6 when we are in row 42 of head 13.3? can’t simply utilize the residual stream ⇒ attention is how we copy info from a row to the other, no?)&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;All answers to be expanded with concrete examples (python, torch, visual diagrams).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Got nerd sniped by copy heads; I couldn’t stop thinking of this meme in the process &amp;amp; thought this applied to them lol&lt;/p&gt;
&lt;img src=&quot;/_astro/do-nothing.CGizK3_X_1PEleW.webp&quot; alt=&quot;do-nothing-win&quot; loading=&quot;eager&quot; decoding=&quot;sync&quot; fetchpriority=&quot;high&quot; width=&quot;400&quot; height=&quot;400&quot;&gt;
&lt;hr class style=&quot;
		border: none;
		border-top: 4px solid var(--text-color);
		opacity: 1;
		margin: 2em 0;
	&quot;&gt;
&lt;p&gt;I found copy heads pretty interesting as I was going through some mechanical interpretability basics; I had jotted down some questions on a piece of paper for testing myself at the end of my learning to get a better intuition behind this concept, and I write this log answering all those questions as a reinforcement of my understanding.&lt;/p&gt;
&lt;h2 id=&quot;definitions&quot;&gt;Definitions&lt;/h2&gt;
&lt;p&gt;Before diving into the mechanics behind copy heads across different transformer layers and their importance, some definitions are necessary to establish the foundations. Some key definitions coming up.&lt;/p&gt;
&lt;h3 id=&quot;copy-heads&quot;&gt;Copy heads&lt;/h3&gt;
&lt;p&gt;Copy heads are attention heads that are identity matrices or are close to being identity matrices. Inside a multi-head attention phase, we split the embedded dimensions into specific number of heads and do a parallel computation: in each head, we calculate the attention weights, do a matmul between attention weights and the &lt;code&gt;V&lt;/code&gt; (here &lt;code&gt;V = x @ W_V&lt;/code&gt;) to calculate attention output and then when we do another matmul between the attention output matrix and a projection matrix &lt;code&gt;W_O&lt;/code&gt;, we get what we call an &lt;code&gt;output projection&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Sometimes, an attention head barely transforms any information that it receives from the &lt;a href=&quot;../inside-a-transformer&quot;&gt;residual stream&lt;/a&gt;; what it outputs could simply be a copy (or close to a copy) of what it receives from the residual stream; in other words, a weighted average of the input and nothing more, nothing less. This sort of an attention head is called a copy head.&lt;/p&gt;
&lt;p&gt;In order to get a clear picture behind a copy head, I think it’s a good idea to get a quick refresher behind how we reach the point of an attention head.&lt;/p&gt;
&lt;h3 id=&quot;refresher-how-did-we-reach-this-rabbit-hole&quot;&gt;Refresher: How did we reach this rabbit hole?&lt;/h3&gt;
&lt;p&gt;An attention head, in a transformer, exists inside a layer (or a block), and is a factor of the transformer’s embedding dimension (&lt;code&gt;d_model&lt;/code&gt; in the original transformer paper) that stores rich representations &amp;amp; semantic features during the training.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I need to add an illustration of a transformer block here&lt;br/&gt;
and also an illustration of attention heads&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let’s assume that I have an &lt;code&gt;input_text&lt;/code&gt; called: &lt;em&gt;“I like hot”&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This is an input word, at the very start of the process, before any embeddings, before any forward passes.&lt;/p&gt;
&lt;p&gt;Parameters that I’d like to assume for the sake of this example:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;batch&lt;/code&gt; = 1 (since we have but a single sentence)&lt;br/&gt;
&lt;code&gt;seqlen&lt;/code&gt; = 3 (sequence length the size of input text)&lt;br/&gt;
&lt;code&gt;d_model&lt;/code&gt; = 32 (this will be the embedding dimension for every word, i.e., input token id)&lt;br/&gt;
&lt;code&gt;n_layers&lt;/code&gt; = 8 (total number of transformer blocks)&lt;br/&gt;
&lt;code&gt;n_heads&lt;/code&gt; = 4 (number of heads in each layer, factor of &lt;code&gt;d_model&lt;/code&gt;, will expand more soon)
&lt;code&gt;vocab_size&lt;/code&gt; = 100&lt;/p&gt;
&lt;p&gt;In a transformer, the &lt;code&gt;input_text&lt;/code&gt; gets tokenized, which is simply a method to assign unique numeric representation to words or characters. Tokenization can be done via various methods, and not all of them break down a sequence of words (i.e., the &lt;code&gt;input_text here&lt;/code&gt;) 1:1 into tokens but for the sake of this worklog, let me assume that each word from the &lt;code&gt;input_text&lt;/code&gt; above corresponds to 1 token, kind of like:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenizer &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;I&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;like&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;hot&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # a simple lookup table, basically a hash map/dict&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is still, to some extent, an oversimplification, but let’s think of tokenization as a step where we create a lookup table (like a dictionary) of words (i.e., tokens) and their unique &lt;code&gt;IDs&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;At this point, if we think of this input sequence of unique token IDs as a tensor, it would be:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenized_input &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenized_input&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# torch.Size([3])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Transformers do computation parallelly in batches, so generally, inputs are tokenized in batches as well. Basically, a single ‘batch’ represents a collection of tokens, and in a normal LLM, during training, there are countless number of batches, and each batch holds an enormous size of text (tons of sentences). In our case, since we have but just &lt;code&gt;input_text&lt;/code&gt;, we are assuming that we have 1 batch, and that means our sequence length will be the length of our tokens, i.e., 3.&lt;/p&gt;
&lt;p&gt;So I unsqueeze the &lt;code&gt;tokenized_input&lt;/code&gt; to better represent it as &lt;code&gt;[batch, seqlen]&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenized_input &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; tokens&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;unsequeeze&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenized_input &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# torch.tensor([[1, 2, 3]])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenized_input&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# torch.Size([1, 3])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So this new &lt;code&gt;tokenized_input&lt;/code&gt; represents the tokenized text that has 1 batch, and the batch’s sequence length is 3.&lt;/p&gt;
&lt;p&gt;Moving on, we initialize random embedding layers/dimensions to each token. In the original tranformer paper, there were 512 embedding dimentions (&lt;code&gt;d_model&lt;/code&gt;) assigned to each token, but for demonstration, let me simply start off with 32 dimensions per token.&lt;/p&gt;
&lt;p&gt;In PyTorch, this is usually done via the &lt;code&gt;torch.nn.Embedding&lt;/code&gt; module, where it could be something like:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;embedding &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;nn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Embedding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt;num_embeddings&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;37000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; embedding_dim&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;512&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;embedding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;weight&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# torch.Size([37000, 512])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;token_embedding &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; embedding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenized_input&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;token_embedding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# torch.Size([1, 3, 512])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, 37000 was the vocab size in the original transformer, and each vocab token had a dimension of 512. &lt;code&gt;torch.nn.Embedding&lt;/code&gt; creates a lookup table (similar to the tokenizer above) of a certain vocabulary size (that we determine based on the kind of tokenizer we have created) where each token ID has its own unique d_model-dimensional vector. Nothing fancy going on here.&lt;/p&gt;
&lt;p&gt;In order to assign random embedding weights for my example, let me think of a vocabulary number. From what I’ve read, it seems that vocab number is generally determined first, and then the tokenizer creates those amounts of tokens and their token ID representations. For a model, any token ID that does not fall inside the tokenizer’s vocab hash map will lead to the model not recognizing it. For me, if I look at the tokenizer has map from above, my &lt;code&gt;vocab_size&lt;/code&gt; will be just 3, but let me extend it a little more and create a &lt;code&gt;vocab_size&lt;/code&gt; of 100:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenizer &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;nice&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;I&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;like&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;hot&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;?&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 98&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;    &amp;quot;coffee&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 99&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Right, so this means I can simply create my token embedding for the &lt;code&gt;tokenized_input&lt;/code&gt; as follows:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;embedding &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;nn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Embedding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 32&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;token_embedding &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; embedding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;tokenized_input&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;token_embedding&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# [1, 3, 32]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So each token now also has its vector weight representation of 32 dimensions.&lt;/p&gt;
&lt;p&gt;We also add positional encoding from here, which I won’t get into details, but the shape of &lt;code&gt;token_embedding&lt;/code&gt; remains the same (&lt;code&gt;[1, 3, 32]&lt;/code&gt;) as a result, and finally, what we have is ready to be fed as the first residual stream to the first multihead attention of the first layer of the transformer.&lt;/p&gt;
&lt;p&gt;These calculations (token ID calculation, tokenization + vocab prep, embedding, positional encoding) are not done more than once. After the resulting embedded + positionally encoded input IDs get fed into the first &lt;code&gt;sublayer&lt;/code&gt;, it is, as the paper&lt;a href=&quot;#footnote-1&quot; class=&quot;footnote-ref&quot;&gt;[1]&lt;/a&gt; I’m reading claims, a residual stream that flows sequentially across each layer of the transformer (layer = transformer block).&lt;/p&gt;
&lt;p&gt;Making some progress here. Now, this &lt;code&gt;token_embedding&lt;/code&gt; goes into the first MHA of the first layer, and something like the following takes place:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;nn &lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; nn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-light-font-style:italic;--shiki-dark:#F9E2AF;--shiki-dark-font-style:italic&quot;&gt; MultiHeadAttention&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-light-font-style:italic;--shiki-dark:#F9E2AF;--shiki-dark-font-style:italic&quot;&gt;nn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#DF8E1D;--shiki-light-font-style:italic;--shiki-dark:#F9E2AF;--shiki-dark-font-style:italic&quot;&gt;Module&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt; __init__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; n_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-light-font-style:italic;--shiki-dark:#CBA6F7;--shiki-dark-font-style:italic&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-light-font-style:italic;--shiki-dark:#CBA6F7;--shiki-dark-font-style:italic&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 32&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; None&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-light-font-style:italic;--shiki-dark:#CBA6F7;--shiki-dark-font-style:italic&quot;&gt;        super&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__init__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;        assert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; n_heads &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;n_heads should be a factor of d_model&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; dtype&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;n_heads &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; n_heads&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_heads &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; n_heads&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_q &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; nn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Linear&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_k &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; nn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Linear&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; nn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Linear&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;        self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_o &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; nn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Linear&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt; forward&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # x == token_embedding&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;            x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;dtype&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        q &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;W_q&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        k &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;W_k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;W_v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        q &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; q&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;n_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        k &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;n_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;n_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        attn_scores &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; q &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        attn_weights &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;softmax&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; dim&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        attn_output &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; attn_weights &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        attn_output_concat &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;            attn_output&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;contiguous&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;        proj_output &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt; self&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;W_o&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_output_concat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;            attn_weights&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;            proj_output&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        )&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a very simple multi-head attention (MHA) mechanism, and this sub-layer exists in each transformer block. Each MHA mechanism involves splitting the provided &lt;code&gt;x&lt;/code&gt; (token_embedding) into &lt;code&gt;n_heads&lt;/code&gt; number of heads to do parallel attention computation (attn_output in the code block above) using &lt;code&gt;d_heads&lt;/code&gt; that are of size &lt;code&gt;d_model // n_heads&lt;/code&gt; (followed immediately by concatenating all these outputs to rejoin all the split heads — basically, in a way, doing &lt;code&gt;n_heads&lt;/code&gt; * &lt;code&gt;d_heads&lt;/code&gt; to get back the original &lt;code&gt;d_model&lt;/code&gt; — and do a matmul with the projection weight &lt;code&gt;W_O&lt;/code&gt; to get the final &lt;code&gt;proj_output&lt;/code&gt;). So, in my demo, &lt;code&gt;d_model&lt;/code&gt; is 32, and if I choose &lt;code&gt;n_heads&lt;/code&gt; as 4, my &lt;code&gt;d_heads&lt;/code&gt; (the dimension that each head gets) becomes 8.&lt;/p&gt;
&lt;p&gt;We’re now with 4 heads per transformer layer, and each head is doing 8 computations in parallel. But is each head that has been computed and concatenated with the other heads doing meaningful transformation with new learned features and semantics, or could it just be calculating a weighted average of the provided input &lt;code&gt;x&lt;/code&gt; and nothing more?&lt;/p&gt;
&lt;p&gt;contd…&lt;/p&gt;
&lt;hr class style=&quot;
		border: none;
		border-top: 1px solid var(--text-color);
		opacity: 0.3;
		margin: 2em 0;
	&quot;&gt;
&lt;div class=&quot;footnotes-section&quot;&gt;&lt;div id=&quot;footnote-1&quot; class=&quot;footnote-item&quot;&gt;&lt;span class=&quot;footnote-number&quot;&gt;[1]&lt;/span&gt;&lt;span&gt;A Mathematical Framework for Transformer Circuits&lt;/a&gt;, Anthropic, 2021. &lt;a href=&apos;https://transformer-circuits.pub/2021/framework/index.html&apos; target=&apos;_blank&apos;&gt;https://transformer-circuits.pub/2021/framework/index.html&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded></item><item><title>Engineering Systems from First Principles</title><link>https://herambve.github.io/blog/engineering-from-first-principles/</link><guid isPermaLink="true">https://herambve.github.io/blog/engineering-from-first-principles/</guid><description>Why I focus on core fundamentals across systems programming, async architectures, and machine learning.</description><pubDate>Mon, 31 Aug 2026 00:00:00 GMT</pubDate><content:encoded>&lt;h2 id=&quot;building-from-the-ground-up&quot;&gt;Building from the Ground Up&lt;/h2&gt;
&lt;p&gt;In an ecosystem where high-level abstractions dominate, there is immense value in understanding what happens underneath the hood:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Systems &amp;amp; Memory:&lt;/strong&gt; How data structures, concurrency, and memory allocation operate in low-level environments like C/C++.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Distributed Backends:&lt;/strong&gt; How asynchronous message queues (Redis &amp;amp; Celery) decouple heavy compute workloads from real-time API latency in FastAPI microservices.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Applied AI:&lt;/strong&gt; How fine-tuned models (e.g. RoBERTa for emotion classification, Whisper for speech-to-text) can be optimized to run efficiently under constrained resource environments.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This blog is where I document my technical experiments, architectural decisions, and worklogs as I build systems and explore new technologies.&lt;/p&gt;
&lt;p&gt;Stay tuned for upcoming deep dives!&lt;/p&gt;</content:encoded></item><item><title>Inside a Transformer: A Worklog</title><link>https://herambve.github.io/blog/inside-a-transformer/</link><guid isPermaLink="true">https://herambve.github.io/blog/inside-a-transformer/</guid><description>explaining to myself how transformer work by looking into its mechanics</description><pubDate>Thu, 30 Oct 2025 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;draft: true&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is currently purely a worklog in progress. Things are not formatted yet.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;on-residual-stream-as-a-communication-channel&quot;&gt;On residual stream as a communication channel&lt;/h2&gt;
&lt;p&gt;Residual stream?&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/_astro/residual-stream.HJx-KztV_Zh8G81.webp&quot; alt=&quot;residual stream image taken from Anthropic&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; fetchpriority=&quot;auto&quot; width=&quot;169&quot; height=&quot;161&quot;&gt;&lt;/p&gt;
&lt;i&gt;&lt;a href=&quot;#footnote-1&quot;&gt;image source&lt;/a&gt;&lt;/i&gt;
&lt;p&gt;Lots about transformer layers (or blocks) &amp;amp; how information flows through them like a ‘stream’, as if different students in a class were given a topic to write an essay about on a long whiteboard &amp;amp; each student writes something new + reads what was written before&lt;/p&gt;
&lt;p&gt;Matters mainly for ‘reading’ and ‘writing’: reading the previous layers’ outputs, and writing one’s output (which also happens to be the output from that nth layer added to the previous layers’ outputs)&lt;/p&gt;
&lt;p&gt;block 0 ⇒ output ⇒ fed to block 1 ⇒ output (enriched with its own + block 0’s info) ⇒ fed to block 3…&lt;/p&gt;
&lt;p&gt;this is a residual stream&lt;/p&gt;
&lt;p&gt;reading = mainly &lt;code&gt;Q @ K^T&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;writing = &lt;code&gt;x = x + attn_output&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&quot;inside-the-attention-sub-layer-the-mechanics&quot;&gt;Inside the attention sub-layer: the mechanics&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Note on sub_layer&lt;/code&gt;: I believe this is the term the original Transformer uses to call its said block’s primary sections. For instance, the encoder has 2 sub-layers: the multihead self-attention (MHA) layer and the the FFN (or MLP) layer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I used to understand the steps involved in the attention sub-layer, but never really got around to understanding the “how” and “why” of it.&lt;/p&gt;
&lt;p&gt;My knowledge of the MHA mechanism, until I went deep into this, was mostly the following steps:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# assumptions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;seqlen &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_head &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 8&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-dark:#CBA6F7&quot;&gt;assert&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_head &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;heads &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_head&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;1. Initialize the weights for the incoming input &lt;code&gt;x&lt;/code&gt; (input = also understood as the residual stream up till that point)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# assuming this already has the embeddings + positional encodings&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-light-font-style:italic;--shiki-dark:#FAB387;--shiki-dark-font-style:italic&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # [1, 3, 32]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_Q &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # [32, 32]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_K &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # [32, 32]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_V &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # [32, 32]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_O &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # [32, 32]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;2. Compute the query, key, and value vectors and split them into heads.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;q &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_Q&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_head&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;k &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_K&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_head&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; heads&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_head&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;q&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# [1, 4, 3, 8], [1, 4, 3, 8], [1, 4, 3, 8]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;3. Compute the attention scores&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; q&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# [1, 4, 3, 3]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;mask should also be applied in case of an attention inside the decoder, omitting for now (sample masking is done later here)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;4. Compute the attention weights&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_weights &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;softmax&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_head &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; dim&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_weights&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# [1, 4, 3, 3]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;5. Compute the attended values&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attended_vals &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; attn_weights&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;v&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attended_vals&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# [1, 4, 3, 8]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;6. Combine the split heads back into the original dimension&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;output_heads_combined &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; attended_vals&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;transpose&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;contiguous&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;view&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;batch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_model&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;output_heads_combined&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# [1, 3, 32]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;7. Compute the output projection&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;output_proj &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; output_heads_combined&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_O&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;output_proj&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape &lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# [1, 3, 32]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But what is the intuition behind these steps? What is the best way of understanding the “how” and “why” of these steps? A few specific &lt;b id=&quot;questions&quot;&gt;questions&lt;/b&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What is the attention weight trying to tell us?&lt;/li&gt;
&lt;li&gt;There are batches of tokens that are being processed in parallel to get the output (input + output_proj, i.e., the updated “residual stream”) from the MHA sub-layer of the transformer. How do I exactly visualize this? What happens sequentially? What happens in parallel?&lt;/li&gt;
&lt;li&gt;Why do we need the output projection? What is the intuition behind it?&lt;/li&gt;
&lt;/ul&gt;
&lt;hr class style=&quot;
		border: none;
		border-top: 1px solid var(--text-color);
		opacity: 0.3;
		margin: 2em 0;
	&quot;&gt;
&lt;h3 id=&quot;trying-to-gain-some-deeper-intuition&quot;&gt;Trying to gain some deeper intuition&lt;/h3&gt;
&lt;p&gt;The Anthropic paper&lt;a href=&quot;#footnote-1&quot; class=&quot;footnote-ref&quot;&gt;[1]&lt;/a&gt; suggests that we break down the MHA sub-layer into 2 “circuits”: the QK circuit and the OV circuit.&lt;/p&gt;
&lt;p&gt;The following is a set of calculations that I did (once again), step-by-step, to gain some deeper intuition.&lt;/p&gt;
&lt;p&gt;1. Initializing some initial initializables&lt;/p&gt;
&lt;p&gt;For this, I will assume the input is: “The cat sat”. I will also assume the &lt;code&gt;d_model = 8&lt;/code&gt;, and &lt;code&gt;d_q[-1] = 4&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;manual_seed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;seqlen &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # because the input is &amp;quot;The cat sat&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # randomly initializing for the sake of quick testing&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_hidden &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # aka d_model&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;q_last_layer &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # aka d_q[-1]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;2. Masking, softmaxing, and getting the attention weights (not going to do head splitting here for the sake of simplicity)&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;upper_triangular &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;triu&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;ones_like&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;mask_bool &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; upper_triangular&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;()[:,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; :]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores_w_causal_mask &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;masked_fill&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; mask_bool&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#8839EF;--shiki-light-font-style:italic;--shiki-dark:#CBA6F7;--shiki-dark-font-style:italic&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&amp;quot;-inf&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_w &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;softmax&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores_w_causal_mask &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; q_last_layer &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;**&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.5&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attn_scores_w_causal_mask&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; attn_scores&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; attn_w&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This gives us:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# attn_scores_w_causal_mask&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.3367&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;    -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;inf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;    -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;inf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.2303&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.1229&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;    -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;inf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2.2082&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6380&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4617&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # attn_scores&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.3367&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.1288&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.2345&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.2303&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.1229&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1863&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2.2082&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6380&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4617&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # attn_w&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6630&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.3370&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6029&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.1453&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.2518&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The following are assumed to be the attention scores, attention scores (with masking), and attention weights for the input “The cat sat”.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.3367&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;    -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;inf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;    -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;inf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.2303&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.1229&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;    -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;inf&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2.2082&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6380&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4617&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.3367&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.1288&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.2345&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.2303&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.1229&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1863&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2.2082&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6380&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4617&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6630&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.3370&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.0000&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6029&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.1453&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.2518&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]]))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Looking at the 3x3 attention weights matrix, I can picture this as:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mtable rowspacing=&quot;0.16em&quot; columnalign=&quot;center center center center&quot; columnlines=&quot;solid none none&quot; columnspacing=&quot;1em&quot; rowlines=&quot;solid none none none&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;The&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;cat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;sat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;The&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;1.0000&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.0000&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.0000&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;cat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.6630&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.3370&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.0000&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;sat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.6029&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.1453&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.2518&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\begin{array}{c|ccc}
 &amp;amp; \text{The} &amp;amp; \text{cat} &amp;amp; \text{sat} \\[0.5em]
\hline \\[0.05em]
\text{The} &amp;amp; 1.0000 &amp;amp; 0.0000 &amp;amp; 0.0000 \\[0.5em]
\text{cat} &amp;amp; 0.6630 &amp;amp; 0.3370 &amp;amp; 0.0000 \\[0.5em]
\text{sat} &amp;amp; 0.6029 &amp;amp; 0.1453 &amp;amp; 0.2518
\end{array}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:7.55em;vertical-align:-3.525em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.025em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:6.025em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mtable&quot;&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-4.485em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;The&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;cat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;sat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;vertical-separator&quot; style=&quot;height:7.55em;border-right-width:0.04em;border-right-style:solid;margin:0 -0.02em;vertical-align:-3.525em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;The&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;1.0000&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.6630&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.6029&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;cat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.0000&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.3370&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.1453&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;sat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.0000&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.0000&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.2518&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-8.35em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:6.025em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;hline&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span id=&quot;intuition-on-attention-weights&quot;&gt;Here, I can think of it like this:&lt;/span&gt;
&lt;ol&gt;
&lt;li&gt;to understand “cat”, how much will I have to look at the token “the”? —&amp;gt; exactly 1.000, so strong relevance for this computation.&lt;/li&gt;
&lt;li&gt;to understand “sat”, how much will I have to look at the token “the”? —&amp;gt; 0.6029, so a pretty solid relevance for this computation.&lt;/li&gt;
&lt;li&gt;to understand “sat”, how much will I have to look at the token “cat”? —&amp;gt; 0.1453, so an okay-ish relevance, not as strong as the one just before.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;3. Computing the attended values&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# assuming:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;X_IN &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;seqlen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_hidden&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt; # [3, 8]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_V_ &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_hidden&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_hidden&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;V &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; X_IN&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_V_&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_O_ &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;randn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;d_hidden&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; d_hidden&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;ATTENDED &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; attn_w&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-light-font-style:italic;--shiki-dark:#FAB387;--shiki-dark-font-style:italic&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-light-font-style:italic;--shiki-dark:#A6E3A1;--shiki-dark-font-style:italic&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&amp;quot;&amp;quot;&amp;quot;W_V: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_V_&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; # [8, 8]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_V_&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;V: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; # [3, 8]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;attended weights: &lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;ATTENDED&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; # [3, 8]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;ATTENDED&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# which gives us something like:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Size&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.0349&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.3211&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.5736&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.8455&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.3123&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.6872&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.0892&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.3553&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.4181&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.8963&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.0499&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  2.2667&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.1790&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.4345&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.3864&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.2862&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.8371&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.9224&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.8113&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.1606&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.3672&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.1754&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.3852&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.4459&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.2024&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.7078&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.0759&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.5357&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.1754&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.5612&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.4527&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.7718&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.1453&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.2311&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.0087&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1423&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.1971&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.1441&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.3383&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.6992&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 2.8140&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.3598&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.0898&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4584&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.5644&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.0563&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.4692&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.4332&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.7281&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.7106&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.6021&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.9604&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4048&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.3543&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.4976&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4747&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1976&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.2683&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.2243&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.0981&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.7423&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.3527&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.2191&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.5526&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;V&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Size&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3.3538&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.6614&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  5.1813&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1024&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.1555&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1970&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.2466&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.8980&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.4393&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.2727&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.4840&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3.8536&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.8261&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  5.3907&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.3668&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.1857&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.3070&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.7861&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  2.5214&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.5471&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.1485&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.0364&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  2.6574&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.5662&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;attended weights&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Size&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3.3538&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.6614&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  5.1813&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1024&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.1555&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.1970&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.2466&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.8980&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.3715&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.3356&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  3.5982&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.3666&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.5123&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  1.6862&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.0399&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.6579&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.1632&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.6177&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  3.8292&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.0111&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.4607&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.9254&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  0.5737&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.2074&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;4. Computing the output projection&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;OUTPUT_PROJECTION &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; ATTENDED&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#04A5E5;--shiki-light-font-style:italic;--shiki-dark:#89DCEB;--shiki-dark-font-style:italic&quot;&gt;__matmul__&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;W_O_&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-light-font-style:italic;--shiki-dark:#FAB387;--shiki-dark-font-style:italic&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-light-font-style:italic;--shiki-dark:#A6E3A1;--shiki-dark-font-style:italic&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;OUTPUT_PROJECTION&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;OUTPUT_PROJECTION&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# which gives us something like:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Size&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 10.8347&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;4.9027&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   7.4389&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.5553&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   9.3776&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.1711&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;14.9518&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;          -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;5.0850&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  8.7450&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3.9095&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   3.6260&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.4063&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   5.4063&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.0348&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;11.8651&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;          -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.1552&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;        [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  9.2331&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;4.1688&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   2.8769&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.0400&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   4.9977&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.7839&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;11.5206&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;          -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.4551&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;5. Combining the attended values and the output projection for the updated &lt;code&gt;residual stream&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;residual_stream &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; X_IN &lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; OUTPUT_PROJECTION&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# this is bascially:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;#   x_in + attended_val @ W_O {here, x_in = (enbeddings + pos_encodings), and attended_val @ W_O = output_projection}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# = x_in + (attention_weights @ V) @ W_O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# = x_in + ((Q @ K.T) @ (x_in @ W_V) @ W_O) {simplifying the attn weights a little here, softmax and being divide by d_k ** 0.5 are omitted}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;residual_stream&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;shape&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; residual_stream&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-light-font-style:italic;--shiki-dark:#9399B2;--shiki-dark-font-style:italic&quot;&gt;# which gives us something like:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;torch&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;Size&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; tensor&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;([[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 12.7616&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3.4154&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   8.3396&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;4.6608&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  10.0561&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;3.4056&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;14.9948&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;           -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;6.6897&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  9.1009&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;4.5961&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   3.1327&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.1649&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   4.2954&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0.9433&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;14.1820&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;           -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.3720&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;         [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;  8.9234&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;4.5645&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   3.6803&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.6616&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;   4.4057&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;1.8470&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;12.3491&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;           -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;2.1242&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]]))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and that’s the output from the MHA sub-layer of the transformer.&lt;/p&gt;
&lt;hr class style=&quot;
		border: none;
		border-top: 1px solid var(--text-color);
		opacity: 0.3;
		margin: 2em 0;
	&quot;&gt;
&lt;h3 id=&quot;a-little-more-intuition-on-what-this-means&quot;&gt;A little more intuition on what this means&lt;/h3&gt;
&lt;p&gt;For, say, the word “sat” (3rd word in the sequence):&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;W_sat = residual_stream[2]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;residual[&amp;quot;sat&amp;quot;] aka residual[2]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;= x_in[&amp;quot;sat&amp;quot;] + attended_val[&amp;quot;sat&amp;quot;] @ W_O&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s assume our attention weight matrix was:&lt;/p&gt;
&lt;span class=&quot;katex-display&quot;&gt;&lt;span class=&quot;katex&quot;&gt;&lt;span class=&quot;katex-mathml&quot;&gt;&lt;math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;&gt;&lt;semantics&gt;&lt;mtable rowspacing=&quot;0.16em&quot; columnalign=&quot;center center center center&quot; columnlines=&quot;solid none none&quot; columnspacing=&quot;1em&quot; rowlines=&quot;solid none none none&quot;&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;The&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;cat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;sat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;The&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;1.00&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.00&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.00&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;cat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.74&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.26&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.00&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mtext&gt;sat&lt;/mtext&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.20&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.50&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;mtd&gt;&lt;mstyle scriptlevel=&quot;0&quot; displaystyle=&quot;false&quot;&gt;&lt;mn&gt;0.30&lt;/mn&gt;&lt;/mstyle&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;annotation encoding=&quot;application/x-tex&quot;&gt;\begin{array}{c|ccc}
 &amp;amp; \text{The} &amp;amp; \text{cat} &amp;amp; \text{sat} \\[0.5em]
\hline \\[0.05em]
\text{The} &amp;amp; 1.00 &amp;amp; 0.00 &amp;amp; 0.00 \\[0.5em]
\text{cat} &amp;amp; 0.74 &amp;amp; 0.26 &amp;amp; 0.00 \\[0.5em]
\text{sat} &amp;amp; 0.20 &amp;amp; 0.50 &amp;amp; 0.30
\end{array}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;&lt;span class=&quot;katex-html&quot; aria-hidden=&quot;true&quot;&gt;&lt;span class=&quot;base&quot;&gt;&lt;span class=&quot;strut&quot; style=&quot;height:7.55em;vertical-align:-3.525em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.025em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:6.025em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mtable&quot;&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-4.485em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;The&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;cat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;sat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;vertical-separator&quot; style=&quot;height:7.55em;border-right-width:0.04em;border-right-style:solid;margin:0 -0.02em;vertical-align:-3.525em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;The&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;1.00&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.74&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.20&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;cat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.00&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.26&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.50&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;col-align-c&quot;&gt;&lt;span class=&quot;vlist-t vlist-t2&quot;&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:4.025em&quot;&gt;&lt;span style=&quot;top:-6.185em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord text&quot;&gt;&lt;span class=&quot;mord&quot;&gt;sat&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-3.235em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.00&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-1.535em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.00&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:0.165em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:3em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;mord&quot;&gt;&lt;span class=&quot;mord&quot;&gt;0.30&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;arraycolsep&quot; style=&quot;width:0.5em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;top:-8.35em&quot;&gt;&lt;span class=&quot;pstrut&quot; style=&quot;height:6.025em&quot;&gt;&lt;/span&gt;&lt;span class=&quot;hline&quot; style=&quot;border-bottom-width:0.04em&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-s&quot;&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;vlist-r&quot;&gt;&lt;span class=&quot;vlist&quot; style=&quot;height:3.525em&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;p&gt;This means:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#E64553;--shiki-light-font-style:italic;--shiki-dark:#EBA0AC;--shiki-dark-font-style:italic&quot;&gt;attention_weights&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-light-font-style:italic;--shiki-dark:#A6E3A1;--shiki-dark-font-style:italic&quot;&gt;sat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.50&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0.30&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, &lt;code&gt;residual[&amp;quot;sat&amp;quot;]&lt;/code&gt; is:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;=&amp;gt; x_in[&amp;quot;sat&amp;quot;] + (attention_weights[&amp;quot;sat&amp;quot;] @ V) @ W_O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;=&amp;gt; x_in[&amp;quot;sat&amp;quot;] + ([0.2, 0.5, 0.3] @ [V_The, V_cat, V_sat]) @ W_O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;=&amp;gt; x_in[&amp;quot;sat&amp;quot;] + (0.2 * V_The + 0.5 * V_cat + 0.3 * V_sat) @ W_O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;=&amp;gt; x_in[&amp;quot;sat&amp;quot;] + (0.2 * x_in[&amp;quot;the&amp;quot;] + 0.5 * x_in[&amp;quot;cat&amp;quot;] + 0.3 * x_in[&amp;quot;sat&amp;quot;]) @ W_O&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hence &lt;code&gt;W_sat&lt;/code&gt; is:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;x_in[&amp;quot;sat&amp;quot;] + (0.2 * x_in[&amp;quot;the&amp;quot;] + 0.5 * x_in[&amp;quot;cat&amp;quot;] + 0.3 * x_in[&amp;quot;sat&amp;quot;]) @ W_O&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;so-basically&quot;&gt;So basically…&lt;/h3&gt;
&lt;p&gt;We can divide this process into 2 parts:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;the QK circuit&lt;/li&gt;
&lt;li&gt;the OV circuit&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# the QK circuit:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Q @ K.T&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;=&amp;gt; (x @ W_Q) @ (x @ W_K).T&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;# the OV circuit:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;(attention_weight @ V) @ W_O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;=&amp;gt; (attention_weight @ (x @ W_V) @ W_O)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;=&amp;gt; (attention_weight @ x) @ (W_V @ W_O)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why talk about &lt;code&gt;W_OV = W_V @ W_O&lt;/code&gt;? =&amp;gt; For analysis and interpretability&lt;/p&gt;
&lt;p&gt;Instead of thinking “first we transform by &lt;code&gt;W_V&lt;/code&gt;, then later by &lt;code&gt;W_O&lt;/code&gt;,” we can think: “This attention head applies a single combined transformation &lt;code&gt;W_OV&lt;/code&gt; to move information.”&lt;/p&gt;
&lt;h3 id=&quot;update-on-the-questions-i-asked-myself-earlier&quot;&gt;Update on the &lt;a href=&quot;#questions&quot;&gt;questions&lt;/a&gt; I asked myself earlier&lt;/h3&gt;
&lt;ol&gt;&lt;li&gt;What is the attention weight trying to tell us? &lt;br/&gt;
=&amp;gt; &lt;a href=&quot;#intuition-on-attention-weights&quot;&gt;I know now&lt;/a&gt; how to think about it.&lt;/li&gt;&lt;li&gt;There are batches of tokens that are being processed in parallel to get the output (input + output_proj, i.e., the updated “residual stream”) from the MHA sub-layer of the transformer. How do I exactly visualize this? What happens sequentially? What happens in parallel? &lt;br/&gt;
=&amp;gt; Need to get my head around this. I’m sure this is much simpler than I’m making it out to be. &lt;mark&gt;Will update this soon.&lt;/mark&gt;&lt;/li&gt;&lt;li&gt;Why do we need the output projection? What is the intuition behind it? &lt;br/&gt;
=&amp;gt; This is basically how much to move the information forward based on the attention weights. &lt;mark&gt;Will update more details on this to come.&lt;/mark&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;Still lots of questions to answer. Will update this as I go along.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr class style=&quot;
		border: none;
		border-top: 1px solid var(--text-color);
		opacity: 0.3;
		margin: 2em 0;
	&quot;&gt;
&lt;div class=&quot;footnotes-section&quot;&gt;&lt;div id=&quot;footnote-1&quot; class=&quot;footnote-item&quot;&gt;&lt;span class=&quot;footnote-number&quot;&gt;[1]&lt;/span&gt;&lt;span&gt;A Mathematical Framework for Transformer Circuits&lt;/a&gt;, Anthropic, 2021. &lt;a href=&apos;https://transformer-circuits.pub/2021/framework/index.html&apos; target=&apos;_blank&apos;&gt;https://transformer-circuits.pub/2021/framework/index.html&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content:encoded></item><item><title>Self-Hosting Git with Gitea + Tailscale + Cloudflare Tunnel</title><link>https://herambve.github.io/blog/self-hosting-git/</link><guid isPermaLink="true">https://herambve.github.io/blog/self-hosting-git/</guid><description>documenting how I turned my mac mini into a self-hosted Git server with Gitea, GitHub pull mirrors, Tailscale, Cloudflare Tunnel, and daily backups</description><pubDate>Thu, 09 Jul 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;draft: true&lt;/p&gt;
&lt;p&gt;(wip)&lt;/p&gt;
&lt;p&gt;I’ve kinda always wanted to keep my own self-hosted backup of my GitHub, so I finally went ahead
and tried it on my mac mini. I used Gitea for the Git server, GitHub pull mirrors for existing
repos, Tailscale for private SSH access, Cloudflare Tunnel for the public web UI, and a few daily
jobs for backup and cleanup.&lt;/p&gt;
&lt;p&gt;I am writing this as I try the setup myself. So this is not meant to be a perfectly detached
checklist copied from docs. It is more like me writing down what I did, where I changed my mind, and
what I would probably keep if I had to set it up again.&lt;/p&gt;
&lt;p&gt;At first, I thought this might be about replacing GitHub. It is not, at least not for me. GitHub is
still good (or at least is still the industry standard) at public repo hosting, pull requests,
comments, issues, stars, releases, and general discovery. What I wanted was another place where my
repos live. A machine I control, reachable privately, visible publicly for read-only browsing,
backed up daily, and restorable if something goes wrong.&lt;/p&gt;
&lt;p&gt;The version I ended up with looks roughly like this.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;GitHub&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  source of truth for GitHub-first repos&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  pull requests, issues, stars, comments&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Gitea on the always-on machine&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  local Git server&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  native GitHub pull mirrors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  private repos&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  public web UI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  daily backups&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Tailscale&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  private SSH Git access&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Cloudflare Tunnel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  public HTTPS access to the Gitea web UI&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;launchd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  keeps Gitea, cloudflared, backups, metadata refresh, and cleanup running&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I am using placeholders throughout the post.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;~/gitea-stack&lt;/code&gt; for the local stack directory&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;github-user&amp;gt;&lt;/code&gt; for the GitHub account&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;gitea-user&amp;gt;&lt;/code&gt; for the Gitea account&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;tailscale-ip&amp;gt;&lt;/code&gt; for the private Tailscale address&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git.example.com&lt;/code&gt; for the public hostname&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I am intentionally not putting real local usernames, private IPs, tunnel IDs, email addresses, token
paths, or secrets in a public post.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;1-what-i-ended-up-building&quot;&gt;1. What I Ended Up Building&lt;/h2&gt;
&lt;p&gt;I ended up thinking about the setup as two kinds of repos.&lt;/p&gt;
&lt;p&gt;The first kind is a GitHub-first repo.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;laptop -&amp;gt; push to GitHub -&amp;gt; Gitea pulls from GitHub&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For repos like this, I let GitHub stay the source of truth. Gitea is a native pull mirror. It keeps
a local copy of branches and tags, and it shows the mirror icon in the UI.&lt;/p&gt;
&lt;p&gt;The second kind is a Gitea-first repo.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;laptop -&amp;gt; push to Gitea&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;optional later step -&amp;gt; publish to GitHub&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For repos like this, I start on the local Git server. They stay local unless I explicitly decide to
publish them to GitHub.&lt;/p&gt;
&lt;p&gt;This split mattered more than I expected.&lt;/p&gt;
&lt;p&gt;When a repo already lives on GitHub and people interact with it there, I prefer to let GitHub stay
in charge. Pull requests, comments, issues, and stars are not Git objects. Mirroring commits alone
does not move that whole social layer.&lt;/p&gt;
&lt;p&gt;When a repo starts locally, Gitea can be the source of truth. I can still publish it to GitHub later
if that becomes useful.&lt;/p&gt;
&lt;p&gt;I kept this shape because it lets GitHub do what GitHub is good at, while still giving me a local
Git home that I can control and back up.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;2-why-gitea&quot;&gt;2. Why Gitea&lt;/h2&gt;
&lt;p&gt;I picked Gitea because it is small and boring in a good way.&lt;/p&gt;
&lt;p&gt;These were the pieces I needed.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Git over SSH&lt;/li&gt;
&lt;li&gt;public and private repos&lt;/li&gt;
&lt;li&gt;a web UI&lt;/li&gt;
&lt;li&gt;native repository mirrors&lt;/li&gt;
&lt;li&gt;SQLite support&lt;/li&gt;
&lt;li&gt;a single binary install path&lt;/li&gt;
&lt;li&gt;easy file-based backup&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This was the mental model I liked.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;launchd -&amp;gt; gitea binary -&amp;gt; SQLite + bare Git repos&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There is no container layer in my version. There is nothing wrong with Docker, but I chose the
direct binary install because it is easy to understand. If something fails, I only have a few places
I usually check.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the Gitea process&lt;/li&gt;
&lt;li&gt;&lt;code&gt;app.ini&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;the SQLite database&lt;/li&gt;
&lt;li&gt;the bare repositories&lt;/li&gt;
&lt;li&gt;the launchd service&lt;/li&gt;
&lt;li&gt;the logs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That felt like a manageable list.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;3-directory-layout&quot;&gt;3. Directory Layout&lt;/h2&gt;
&lt;p&gt;I kept the stack under one directory.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;~/gitea-stack/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  bin/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  custom/conf/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  data/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  repositories/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  log/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  run/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  scripts/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  secrets/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  metadata/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  lists/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each directory had a clear job for me.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bin/&lt;/code&gt; for the Gitea binary&lt;/li&gt;
&lt;li&gt;&lt;code&gt;custom/conf/&lt;/code&gt; for Gitea config&lt;/li&gt;
&lt;li&gt;&lt;code&gt;data/&lt;/code&gt; for the SQLite database and Gitea state&lt;/li&gt;
&lt;li&gt;&lt;code&gt;repositories/&lt;/code&gt; for bare Git repos&lt;/li&gt;
&lt;li&gt;&lt;code&gt;log/&lt;/code&gt; for service and job logs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;run/&lt;/code&gt; for lock files&lt;/li&gt;
&lt;li&gt;&lt;code&gt;scripts/&lt;/code&gt; for helper commands&lt;/li&gt;
&lt;li&gt;&lt;code&gt;secrets/&lt;/code&gt; for tokens and generated secrets&lt;/li&gt;
&lt;li&gt;&lt;code&gt;metadata/&lt;/code&gt; for GitHub metadata snapshots&lt;/li&gt;
&lt;li&gt;&lt;code&gt;lists/&lt;/code&gt; for generated repo lists&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This layout made backup simpler. Most state lives in one directory. Logs and temporary files can be
excluded. I add anything outside the stack, such as SSH key material used by the service, to the
backup explicitly.&lt;/p&gt;
&lt;p&gt;I created the base directories with this command.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -p&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/{bin,custom/conf,data,repositories,log,run,scripts,secrets,metadata,lists}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 700&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/secrets&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;4-installing-gitea&quot;&gt;4. Installing Gitea&lt;/h2&gt;
&lt;p&gt;I installed Gitea from the official binary.&lt;/p&gt;
&lt;p&gt;On Apple Silicon, I used the &lt;code&gt;darwin-arm64&lt;/code&gt; build. On Intel Macs, the equivalent would be
&lt;code&gt;darwin-amd64&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/bin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -L&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;https://dl.gitea.com/gitea/&amp;lt;version&amp;gt;/gitea-&amp;lt;version&amp;gt;-darwin-arm64&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -L&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea.asc&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;https://dl.gitea.com/gitea/&amp;lt;version&amp;gt;/gitea-&amp;lt;version&amp;gt;-darwin-arm64.asc&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; +x&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I verified the signature.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gpg&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --keyserver&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; hkps://keys.openpgp.org&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --recv&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; 7C9E68152594688862D62AF62D9AE806EC1592E2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gpg&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --verify&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea.asc&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I also checked that Git existed.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --version&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Gitea shells out to Git, so the machine needs Git installed too.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;5-first-run-and-web-install&quot;&gt;5. First Run And Web Install&lt;/h2&gt;
&lt;p&gt;I ran Gitea manually first.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;GITEA_WORK_DIR&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;~/gitea-stack&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;  ~/gitea-stack/bin/gitea&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; web&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;  --config&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/custom/conf/app.ini&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;  --work-path&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point I opened the installer only on a private address. I did not want the first install
screen exposed publicly.&lt;/p&gt;
&lt;p&gt;These were the basic settings I used.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;database set to SQLite&lt;/li&gt;
&lt;li&gt;repository root set to &lt;code&gt;~/gitea-stack/repositories&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;app data path set to &lt;code&gt;~/gitea-stack/data&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;web port set to &lt;code&gt;3000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;SSH port set to &lt;code&gt;2222&lt;/code&gt; or another non-default port&lt;/li&gt;
&lt;li&gt;registration disabled after the first admin user is created&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After the first install, I locked down the config.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 600&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/custom/conf/app.ini&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I restarted Gitea and checked the health endpoint.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; http://&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;private-addres&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;:3000/api/healthz&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this stage, I still kept the server private.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;6-moving-secrets-out-of-appini&quot;&gt;6. Moving Secrets Out Of app.ini&lt;/h2&gt;
&lt;p&gt;Gitea creates several secret values during setup. I did not want those values pasted directly into
&lt;code&gt;app.ini&lt;/code&gt;, so I moved them into files under &lt;code&gt;secrets/&lt;/code&gt; and pointed &lt;code&gt;app.ini&lt;/code&gt; to those files.&lt;/p&gt;
&lt;p&gt;The config supports file URIs.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;ini&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;SECRET_KEY_URI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; file:/path/to/secrets/secret-key&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;INTERNAL_TOKEN_URI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; file:/path/to/secrets/internal-token&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;JWT_SECRET_URI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; file:/path/to/secrets/oauth2-jwt-secret&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;LFS_JWT_SECRET_URI&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt; file:/path/to/secrets/lfs-jwt-secret&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The real paths are absolute local paths on the machine. I am not putting those exact paths in a
public post.&lt;/p&gt;
&lt;p&gt;I locked the files down.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 700&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/secrets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 600&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/secrets/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I wanted &lt;code&gt;app.ini&lt;/code&gt; to describe where secrets live, not become the place where every secret value is
pasted forever.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;7-running-gitea-with-launchd&quot;&gt;7. Running Gitea With launchd&lt;/h2&gt;
&lt;p&gt;On macOS, I used &lt;code&gt;launchd&lt;/code&gt; to run Gitea after reboot.&lt;/p&gt;
&lt;p&gt;The service runs this command.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;/gitea-stack/bin/gitea web &lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;\&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;  --config&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/custom/conf/app.ini&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt; \&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;  --work-path&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The LaunchDaemon has these settings.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;RunAtLoad&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;KeepAlive&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;working directory set to &lt;code&gt;~/gitea-stack&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;stdout path set to &lt;code&gt;~/gitea-stack/log/launchd.out.log&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;stderr path set to &lt;code&gt;~/gitea-stack/log/launchd.err.log&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;user set to a normal local user, not root&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After installing the plist, I checked it.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; launchctl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; print&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; system/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;service-nam&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I checked Gitea.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;curl&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; http://&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;private-addres&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;:3000/api/healthz&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I also rebooted the machine and checked again. A server that does not survive reboot does not feel
finished to me.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;8-private-ssh-access-with-tailscale&quot;&gt;8. Private SSH Access With Tailscale&lt;/h2&gt;
&lt;p&gt;I used Tailscale before doing anything public.&lt;/p&gt;
&lt;p&gt;This was the first networking choice I made because it gave me a private path before anything was on
the public internet.&lt;/p&gt;
&lt;p&gt;This was the private path.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;laptop -&amp;gt; Tailscale -&amp;gt; Mac mini -&amp;gt; Gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This let another laptop clone and push without SSH-ing into the Mac mini first.&lt;/p&gt;
&lt;p&gt;On the laptop, I created a dedicated SSH key.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;ssh-keygen&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -t&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ed25519&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -f&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/.ssh/id_ed25519_gitea&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -C&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;gitea&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I added the public key to Gitea.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;cat&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/.ssh/id_ed25519_gitea.pub&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My SSH config entry looks like this.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Host gitea&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  HostName &amp;lt;tailscale-ip&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  Port 2222&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  User git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  IdentityFile ~/.ssh/id_ed25519_gitea&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  IdentitiesOnly yes&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I tested it with this command.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;ssh&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -T&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I set a repo remote.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; remote&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; set-url&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;gitea-use&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;rep&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;o&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;.git&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At this point, private Git over SSH worked through Tailscale.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;9-choosing-the-mirror-direction&quot;&gt;9. Choosing The Mirror Direction&lt;/h2&gt;
&lt;p&gt;There are two possible mirror directions.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;Gitea -&amp;gt; GitHub&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;GitHub -&amp;gt; Gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For GitHub-first repos, I chose GitHub ⇒ Gitea.&lt;/p&gt;
&lt;p&gt;GitHub is more than a Git remote. It also holds pull requests, comments, issues, stars, releases,
and public repo pages. When the repo’s public activity already happens on GitHub, I think GitHub is
the better source of truth.&lt;/p&gt;
&lt;p&gt;So Gitea pulls from GitHub.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;push to GitHub -&amp;gt; Gitea pulls from GitHub&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I liked this direction because it gave me the things I cared about.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;native mirror icon in Gitea&lt;/li&gt;
&lt;li&gt;GitHub PRs staying on GitHub&lt;/li&gt;
&lt;li&gt;GitHub comments staying on GitHub&lt;/li&gt;
&lt;li&gt;public repo discovery staying on GitHub&lt;/li&gt;
&lt;li&gt;no custom push hooks for GitHub-source repos&lt;/li&gt;
&lt;li&gt;no write deploy keys for every GitHub-source repo&lt;/li&gt;
&lt;li&gt;a local mirror and backup on the Mac mini&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For Gitea-first repos, Gitea ⇒ GitHub can still make sense. The important part for me is that
publishing to GitHub stays explicit.&lt;/p&gt;
&lt;p&gt;I started with the idea of pushing from Gitea to GitHub for everything. I moved away from that
because it made GitHub’s pull request and issue layer feel secondary, even though most public repo
activity already happens there.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;10-creating-native-pull-mirrors&quot;&gt;10. Creating Native Pull Mirrors&lt;/h2&gt;
&lt;p&gt;For an existing GitHub repo, I created a native Gitea pull mirror.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;GitHub repo -&amp;gt; Gitea migration -&amp;gt; native pull mirror&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For public repos, the GitHub URL was enough.&lt;/p&gt;
&lt;p&gt;For private repos, Gitea needs credentials that can read the repo. I used a fine-grained GitHub
token with read-only contents access. I did not give it write access.&lt;/p&gt;
&lt;p&gt;I stored the token in a protected file.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;~/gitea-stack/secrets/github-pull-mirror-token&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then I locked it down.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;chmod&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 600&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/gitea-stack/secrets/github-pull-mirror-token&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I did not put this token in &lt;code&gt;.zshrc&lt;/code&gt;, dotfiles, shell exports, or public scripts.&lt;/p&gt;
&lt;p&gt;For automatic imports, I wanted this behavior.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;list GitHub repos&lt;/li&gt;
&lt;li&gt;skip forks&lt;/li&gt;
&lt;li&gt;preserve public/private visibility&lt;/li&gt;
&lt;li&gt;create native pull mirrors for missing repos&lt;/li&gt;
&lt;li&gt;use the read-only token for private repos&lt;/li&gt;
&lt;li&gt;avoid write deploy keys unless the repo is Gitea-first&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I set the mirror interval to something short.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;15 minutes&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A webhook would make syncing more instant, but it also adds another public integration to secure. A
short pull interval felt good enough.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;11-handling-gitea-first-repos&quot;&gt;11. Handling Gitea-First Repos&lt;/h2&gt;
&lt;p&gt;For a new repo that starts on Gitea, this is my flow.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; init&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; repo-name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#D20F39;--shiki-light-font-style:italic;--shiki-dark:#F38BA8;--shiki-dark-font-style:italic&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; repo-name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; remote&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;gitea-use&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;/repo-name.git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; main&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I set push-created repos to default to private.&lt;/p&gt;
&lt;p&gt;When I want the repo to appear on GitHub later, I publish it deliberately.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-push-to-github&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; repo-name&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-push-to-github&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --public&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; repo-name&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A Gitea-to-GitHub push hook fits this case. The repo started on Gitea, so pushing it to GitHub is
an intentional step instead of the default behavior for everything.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;12-github-metadata&quot;&gt;12. GitHub Metadata&lt;/h2&gt;
&lt;p&gt;Git stores commits, branches, tags, and files.&lt;/p&gt;
&lt;p&gt;It does not store GitHub descriptions, topics, stars, homepage URLs, archived status, or visibility
in a way Gitea automatically reuses.&lt;/p&gt;
&lt;p&gt;So I kept a metadata refresh step.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;GitHub API -&amp;gt; metadata JSON -&amp;gt; selected fields applied to Gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These are the fields I cared about.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;description&lt;/li&gt;
&lt;li&gt;homepage URL&lt;/li&gt;
&lt;li&gt;topics&lt;/li&gt;
&lt;li&gt;star count&lt;/li&gt;
&lt;li&gt;archived state&lt;/li&gt;
&lt;li&gt;visibility&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I did not fake GitHub stars as Gitea stars. I stored the GitHub star count as metadata instead.&lt;/p&gt;
&lt;p&gt;I run this daily, but only write to Gitea when something changed. Otherwise every repo starts
looking recently updated even when no code changed.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;13-repository-updated-time&quot;&gt;13. Repository Updated Time&lt;/h2&gt;
&lt;p&gt;Gitea’s repository list has an &lt;code&gt;Updated ... ago&lt;/code&gt; value.&lt;/p&gt;
&lt;p&gt;For a mirror-heavy setup, this can become noisy. Mirror syncs and metadata refreshes can bump the
repository timestamp even if the latest commit did not change.&lt;/p&gt;
&lt;p&gt;I wanted the repo list to mean this.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;repo list updated time = newest branch commit time&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So I added a small normalizer.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;every 60 seconds:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  for each repo:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    find newest branch commit time&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    set repository updated time to that&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I left the native mirror sync timestamp alone. Gitea can still show when the mirror last synced on
the repo page.&lt;/p&gt;
&lt;p&gt;So there are two separate signals.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;repo list for last commit activity&lt;/li&gt;
&lt;li&gt;mirror banner for last mirror sync&lt;/li&gt;
&lt;/ul&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;14-daily-jobs&quot;&gt;14. Daily Jobs&lt;/h2&gt;
&lt;p&gt;The daily automation ended up as this schedule.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;every 60 seconds:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  normalize repository updated times&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;03:05:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  discover new non-fork GitHub repos and import missing ones&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;03:20:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  refresh GitHub metadata&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;03:45:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  create backup and run maintenance&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The discovery job does not sync existing repos. Native mirrors already do that on their own
interval.&lt;/p&gt;
&lt;p&gt;The discovery job only answers one question.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;is there a new non-fork GitHub repo missing from Gitea?&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When there is one, the job imports it as a native pull mirror.&lt;/p&gt;
&lt;p&gt;I care about this because backups only include what exists in Gitea. If a new GitHub repo never
gets imported, the Gitea backup cannot include it.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;15-backups&quot;&gt;15. Backups&lt;/h2&gt;
&lt;p&gt;My backup includes these files.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Gitea config&lt;/li&gt;
&lt;li&gt;SQLite database&lt;/li&gt;
&lt;li&gt;repositories&lt;/li&gt;
&lt;li&gt;metadata&lt;/li&gt;
&lt;li&gt;secrets&lt;/li&gt;
&lt;li&gt;required SSH key material&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It excludes these files.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;logs&lt;/li&gt;
&lt;li&gt;temp files&lt;/li&gt;
&lt;li&gt;cache dirs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The backup job does a few things.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;stop Gitea briefly&lt;/li&gt;
&lt;li&gt;create a &lt;code&gt;.tar.gz&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;start Gitea again through launchd&lt;/li&gt;
&lt;li&gt;run maintenance&lt;/li&gt;
&lt;li&gt;keep the newest 30 backups&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also run a restore check. Creating archives is not enough.&lt;/p&gt;
&lt;p&gt;The restore check does this.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;extract the newest archive into a temp dir&lt;/li&gt;
&lt;li&gt;check that config, database, repos, metadata, and secrets exist&lt;/li&gt;
&lt;li&gt;run SQLite &lt;code&gt;quick_check&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;sample bare repos with &lt;code&gt;git fsck&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;delete the temp extraction&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That gave me more confidence than just seeing archive files pile up.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;16-maintenance&quot;&gt;16. Maintenance&lt;/h2&gt;
&lt;p&gt;The large files I found were not scripts.&lt;/p&gt;
&lt;p&gt;They were old temp clones, migration bundles, and logs.&lt;/p&gt;
&lt;p&gt;My maintenance job does this.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;keep the newest 30 backups&lt;/li&gt;
&lt;li&gt;remove stale metadata for deleted repos&lt;/li&gt;
&lt;li&gt;remove old sync/import/migration temp dirs&lt;/li&gt;
&lt;li&gt;remove old restore-check temp dirs&lt;/li&gt;
&lt;li&gt;remove old import logs&lt;/li&gt;
&lt;li&gt;trim large launchd stdout/stderr logs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It keeps the small server small.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;17-public-access-with-cloudflare-tunnel&quot;&gt;17. Public Access With Cloudflare Tunnel&lt;/h2&gt;
&lt;p&gt;Tailscale handles private access. Cloudflare Tunnel handles public web access.&lt;/p&gt;
&lt;p&gt;I added Cloudflare only after the private Tailscale setup worked. That made the steps easier to
debug. First I made Git work privately, then I made the web UI public.&lt;/p&gt;
&lt;p&gt;I was partly inspired by this &lt;a href=&quot;https://kennethnym.com/blog/a-gentle-introduction-to-cloudflare-tunnels/&quot;&gt;blog post&lt;/a&gt;,
which explains the model nicely.&lt;/p&gt;
&lt;p&gt;The high-level path became this.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;browser -&amp;gt; Cloudflare -&amp;gt; cloudflared on the Git server -&amp;gt; local Gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Git server does not need an inbound router port. &lt;code&gt;cloudflared&lt;/code&gt; opens an outbound connection to
Cloudflare, and Cloudflare routes the public hostname through that connection.&lt;/p&gt;
&lt;p&gt;This was the basic flow I used.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;cloudflared&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; tunnel&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; login&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;cloudflared&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; tunnel&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; create&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;cloudflared&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; tunnel&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; route&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; dns&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; git.example.com&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;cloudflared&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; tunnel&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My config looked like this.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;yaml&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;tunnel&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;lt;tunnel-uuid&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;credentials-file&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; ~/.cloudflared/&amp;lt;tunnel-uuid&amp;gt;.json&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;ingress&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; hostname&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; git.example.com&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;    service&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; http://127.0.0.1:3000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;  -&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt; service&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; http_status:404&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For my actual setup, I stored the credentials file in a protected secrets directory and ran
&lt;code&gt;cloudflared&lt;/code&gt; with launchd.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;18-public-web-private-ssh&quot;&gt;18. Public Web, Private SSH&lt;/h2&gt;
&lt;p&gt;I kept web and SSH separate.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;public web&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  https://git.example.com&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  Cloudflare Tunnel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;private Git SSH&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  gitea:&amp;lt;gitea-user&amp;gt;/&amp;lt;repo&amp;gt;.git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;  Tailscale&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The public internet can view public repos.&lt;/p&gt;
&lt;p&gt;Git pushes still go through Tailscale.&lt;/p&gt;
&lt;p&gt;This leaves the Gitea SSH server private. I do not have a reason to expose it publicly right now.&lt;/p&gt;
&lt;p&gt;Gitea still enforces permissions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;registration disabled&lt;/li&gt;
&lt;li&gt;private repos hidden from anonymous users&lt;/li&gt;
&lt;li&gt;HTTP Git disabled&lt;/li&gt;
&lt;li&gt;basic auth disabled&lt;/li&gt;
&lt;li&gt;admin 2FA enabled&lt;/li&gt;
&lt;li&gt;reverse-proxy auth disabled&lt;/li&gt;
&lt;li&gt;webhooks disabled unless needed&lt;/li&gt;
&lt;li&gt;config and secret files locked down&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cloudflare gets traffic to the Git server. Gitea decides what each visitor can see.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;19-security-checklist&quot;&gt;19. Security Checklist&lt;/h2&gt;
&lt;p&gt;Before I called the setup done, I checked these things.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;production mode is on&lt;/li&gt;
&lt;li&gt;registration is disabled&lt;/li&gt;
&lt;li&gt;push-created repos default to private&lt;/li&gt;
&lt;li&gt;public repos are visible to anonymous users&lt;/li&gt;
&lt;li&gt;private repos are hidden from anonymous users&lt;/li&gt;
&lt;li&gt;HTTP Git is disabled&lt;/li&gt;
&lt;li&gt;SSH is not public&lt;/li&gt;
&lt;li&gt;browser login works&lt;/li&gt;
&lt;li&gt;admin 2FA is enabled&lt;/li&gt;
&lt;li&gt;generated secrets live in files&lt;/li&gt;
&lt;li&gt;secret files are mode &lt;code&gt;600&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;secret directory is mode &lt;code&gt;700&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Gitea config is mode &lt;code&gt;600&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Cloudflare tunnel credentials are mode &lt;code&gt;600&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;private GitHub mirror token is read-only&lt;/li&gt;
&lt;li&gt;GitHub-source repos have no active push hooks&lt;/li&gt;
&lt;li&gt;backups are mode &lt;code&gt;600&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;restore check passes&lt;/li&gt;
&lt;li&gt;audit passes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Self-hosting does not help much if tokens leak into shell files, public notes, logs, or old backup
archives.&lt;/p&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;20-landing-page-and-short-links&quot;&gt;20. Landing Page And Short Links&lt;/h2&gt;
&lt;p&gt;Gitea repo paths look like this.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;https://git.example.com/&amp;lt;gitea-user&amp;gt;/&amp;lt;repo&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The default path is fine, but I wanted the public root page to feel more like a profile page. Gitea
can show a profile README through a &lt;code&gt;.profile&lt;/code&gt; repo.&lt;/p&gt;
&lt;p&gt;For short repo links, I added a small Cloudflare Worker.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;https://git.example.com/repo-name&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It redirects to this.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;https://git.example.com/&amp;lt;gitea-user&amp;gt;/repo-name&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I made sure the Worker does not redirect real Gitea paths.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/user&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/api&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/assets&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/explore&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/&amp;lt;gitea-user&amp;gt;/...&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr/&gt;
&lt;p&gt;draft: true&lt;/p&gt;
&lt;h2 id=&quot;21-normal-workflow&quot;&gt;21. Normal Workflow&lt;/h2&gt;
&lt;p&gt;For a GitHub-first repo, I push to GitHub.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; main&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Gitea pulls the change on the mirror interval.&lt;/p&gt;
&lt;p&gt;For a new GitHub repo, I let the import job bring it into Gitea.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-import-new-github-repos&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --dry-run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-import-new-github-repos&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For a Gitea-first repo, I push to Gitea.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; remote&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; gitea:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;gitea-use&lt;/span&gt;&lt;span style=&quot;--shiki-light:#4C4F69;--shiki-dark:#CDD6F4&quot;&gt;r&lt;/span&gt;&lt;span style=&quot;--shiki-light:#179299;--shiki-dark:#94E2D5&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;/repo-name.git&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; push&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; -u&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; origin&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; main&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To publish a Gitea-first repo to GitHub, I run this command.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-push-to-github&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; --private&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; repo-name&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For checks, I use these commands.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-audit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-security-check&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-restore-check&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For a manual backup, I use this command.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;zsh&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-light-font-style:italic;--shiki-dark:#89B4FA;--shiki-dark-font-style:italic&quot;&gt;gitea-backup&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The setup has landed here for me. GitHub stays for now for public collaboration (@Cursor yall
completing your GitHub killer anytime soon?), Gitea for a local mirror and Git home, Tailscale for
private SSH, Cloudflare Tunnel for public web, and daily jobs for the boring parts.&lt;/p&gt;</content:encoded></item><item><title>Attention-Only vs. All-Layer LoRA: A Worklog</title><link>https://herambve.github.io/blog/lora-target-module-worklog/</link><guid isPermaLink="true">https://herambve.github.io/blog/lora-target-module-worklog/</guid><description>a longer worklog from narrowing, cleaning, training, and evaluating a Qwen3-8B LoRA target-module comparison</description><pubDate>Fri, 15 May 2026 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;draft: true&lt;/p&gt;
&lt;p&gt;This is the worklog version of the &lt;a href=&quot;/projects/lora-and-friends&quot;&gt;LoRA and Friends&lt;/a&gt; experiment.&lt;/p&gt;
&lt;p&gt;The final public artifacts are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;model checkpoints: &lt;a href=&quot;https://huggingface.co/sumitdotml/lora-and-friends&quot;&gt;sumitdotml/lora-and-friends&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;dataset: &lt;a href=&quot;https://huggingface.co/datasets/sumitdotml/lora-and-friends-dataset&quot;&gt;sumitdotml/lora-and-friends-dataset&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;1-before-the-run&quot;&gt;1. Before The Run&lt;/h2&gt;
&lt;h3 id=&quot;2026-04-19-the-first-plan-had-too-many-moving-parts&quot;&gt;2026-04-19: the first plan had too many moving parts&lt;/h3&gt;
&lt;p&gt;I started with too many possible experiments in the air: full fine-tuning, MoE, RL, transfer evaluation, Tinker defaults, LoRA target modules, dataset choice, benchmark choice, and budget. That was not a runnable project but a pile of directions.&lt;/p&gt;
&lt;p&gt;I removed most of it. Phase one became:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;one model: &lt;code&gt;Qwen3-8B&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;one task family: math reasoning&lt;/li&gt;
&lt;li&gt;one final benchmark anchor: &lt;code&gt;GSM8K&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;one supervised fine-tuning comparison: attention-only LoRA vs all-layer LoRA&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I kept &lt;code&gt;Qwen3-8B&lt;/code&gt; as the working model. &lt;code&gt;Qwen3-8B-Base&lt;/code&gt; stayed in reserve for a different post-training story, and a smaller future model would have added a model-choice variable that I did not want in the first comparison.&lt;/p&gt;
&lt;p&gt;The target-module comparison was the part I wanted to isolate:&lt;/p&gt;

















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;condition&lt;/th&gt;&lt;th&gt;intended adapter scope&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td&gt;attention projections&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td&gt;attention and MLP projections&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;That meant everything else had to become boring on purpose: same base model, same data, same rank, same training schedule, same evaluation prompt, same answer extraction, same benchmark, and same checkpoint-selection rule.&lt;/p&gt;
&lt;h3 id=&quot;why-gsm8k-became-the-benchmark-not-the-main-training-set&quot;&gt;Why GSM8K became the benchmark, not the main training set&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;GSM8K&lt;/code&gt; was tempting as a training source because it is clean and familiar. But the train split has only &lt;code&gt;7,473&lt;/code&gt; rows, and the test split has &lt;code&gt;1,319&lt;/code&gt; rows. I wanted the final score to be held out and easy to explain, so I kept GSM8K as the benchmark anchor.&lt;/p&gt;
&lt;p&gt;The training source moved to &lt;code&gt;nvidia/OpenMathInstruct-2&lt;/code&gt;, especially the &lt;code&gt;train_1M&lt;/code&gt; split. The schema was simple enough to render into chat format:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;problem&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;Solve for $y$:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;$$&lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;\\&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;frac{y^2 - 3y + 2}{y - 2} = y + 1$$&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;generated_solution&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;... &lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;\\&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;[ y = &lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;\\&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;boxed{2} &lt;/span&gt;&lt;span style=&quot;--shiki-light:#EA76CB;--shiki-dark:#F5C2E7&quot;&gt;\\&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt;]&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;expected_answer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;2&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;problem_source&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;augmented_math&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I also looked at &lt;code&gt;unsloth/OpenMathReasoning-mini&lt;/code&gt;. It had more character and much longer traces, but the token profile made it harder to fit into the budget. &lt;code&gt;OpenMathInstruct-2&lt;/code&gt; looked less exciting, but it had the right shape for a controlled SFT comparison.&lt;/p&gt;
&lt;p&gt;The first sizing pass made the budget concrete. A 500-row tokenizer sample from OpenMathInstruct-2 averaged &lt;code&gt;456.9&lt;/code&gt; rendered tokens per example, with &lt;code&gt;p90 = 813&lt;/code&gt; and &lt;code&gt;p95 = 963&lt;/code&gt;. At Tinker’s then-current &lt;code&gt;Qwen3-8B&lt;/code&gt; training price of &lt;code&gt;$0.40 / M&lt;/code&gt; tokens, a 30k-example run started to look feasible.&lt;/p&gt;
&lt;p&gt;The first target was a mildly balanced 30k dataset:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;30k working raw dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- 21,000  augmented_math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-  7,000  augmented_gsm8k&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-  1,000  math&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;-  1,000  gsm8k&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first plan did not survive.&lt;/p&gt;
&lt;h3 id=&quot;the-first-run-sheet-was-useful-even-though-it-was-wrong&quot;&gt;The first run sheet was useful even though it was wrong&lt;/h3&gt;
&lt;p&gt;The first run sheet had a training split of &lt;code&gt;27,000&lt;/code&gt; rows and a validation split of &lt;code&gt;3,000&lt;/code&gt; rows. With the early weighted mean estimate, that came out to about &lt;code&gt;11.70M&lt;/code&gt; train tokens per epoch and about &lt;code&gt;$4.68&lt;/code&gt; per epoch.&lt;/p&gt;
&lt;p&gt;The pilot shape was also already there:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;pilot sweep&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- train rows: 5,000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- val rows:     500&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- 2 conditions x 3 LR values x 1 seed x 1 epoch&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the main comparison shape was visible:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;thesis comparison&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- 2 conditions x 3 seeds x 2 epochs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;- total train cost: about $56.17&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Those numbers were later superseded, but they were still useful. They forced me to think in runs, seeds, rows, tokens, and dollars instead of treating “fine-tune a model” as one vague action.&lt;/p&gt;
&lt;p&gt;The benchmark side looked cheap in comparison. One full GSM8K eval was estimated around cents, not dollars of training cost. So from the beginning, training data and training schedule were the real budget levers.&lt;/p&gt;
&lt;h2 id=&quot;2-before-training&quot;&gt;2. Before Training&lt;/h2&gt;
&lt;h3 id=&quot;2026-04-20-the-first-dataset-looked-usable-until-i-read-it&quot;&gt;2026-04-20: the first dataset looked usable until I read it&lt;/h3&gt;
&lt;p&gt;The initial &lt;code&gt;openmath_30k&lt;/code&gt; artifacts matched the recipe exactly: &lt;code&gt;27,000&lt;/code&gt; train rows and &lt;code&gt;3,000&lt;/code&gt; validation rows. Structurally it looked clean. Every inspected training row had a final &lt;code&gt;\boxed{...}&lt;/code&gt; answer. The first rendered dataset under &lt;code&gt;qwen3_disable_thinking&lt;/code&gt; also looked reasonable.&lt;/p&gt;
&lt;p&gt;Then the manual audit started doing its job.&lt;/p&gt;
&lt;p&gt;One sampled &lt;code&gt;augmented_gsm8k&lt;/code&gt; row reasoned itself into clipping a percentage answer to &lt;code&gt;100&lt;/code&gt; because a fictional budget was too small. A tetrahedron row noticed a fractional tetrahedron count was impossible and still pushed through to a final ratio. A pattern search found disclaimer-like repair language:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;cannot spend more than she has&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;doesn&amp;#39;t align with the logical outcome&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;we need to set the value to 100&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At first I treated this as a light cleanup problem. I removed a couple of flagged rows, built filtered artifacts, and added manual review packs. The mismatch count fell, the obvious bad rows got smaller, and the dataset felt closer.&lt;/p&gt;
&lt;p&gt;But each pass exposed another class of issue.&lt;/p&gt;
&lt;h3 id=&quot;the-augmented-branch-kept-asking-for-another-pass&quot;&gt;The augmented branch kept asking for another pass&lt;/h3&gt;
&lt;p&gt;The suspicious rows were concentrated in the augmented sources. The failures were not all the same, but they had a family resemblance: impossible integer counts, prompt-generation residue, unsupported case-bashing, and answers that looked right only because the generated reasoning had quietly changed the problem.&lt;/p&gt;
&lt;p&gt;The review packs were small enough to inspect but large enough to change my mind. A &lt;code&gt;53&lt;/code&gt;-row suspicious train pack for &lt;code&gt;curated_v2&lt;/code&gt; produced &lt;code&gt;29&lt;/code&gt; removals. A later &lt;code&gt;61&lt;/code&gt;-row suspicious train pack produced &lt;code&gt;30&lt;/code&gt; hard removals, all from &lt;code&gt;augmented_math&lt;/code&gt;. These were not tiny formatting mismatches but rows with broken reasoning, contaminated prompts, or invalid proofs.&lt;/p&gt;
&lt;p&gt;The curation sequence became longer than I wanted:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;curated_v2&lt;/code&gt;: after the first train and validation suspicious-row removals&lt;/li&gt;
&lt;li&gt;&lt;code&gt;curated_v3&lt;/code&gt;: after another suspicious train review&lt;/li&gt;
&lt;li&gt;&lt;code&gt;curated_v4&lt;/code&gt;: after validation cleanup&lt;/li&gt;
&lt;li&gt;&lt;code&gt;curated_v5&lt;/code&gt;: after backfilling weak &lt;code&gt;augmented_math&lt;/code&gt; rows&lt;/li&gt;
&lt;li&gt;&lt;code&gt;curated_v6&lt;/code&gt;: after removing prompt-generation debris like &lt;code&gt;A new problem:&lt;/code&gt; and &lt;code&gt;The new problem is:&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One excerpt from the log captures the real issue:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;The problem was no longer &amp;quot;find one more regex,&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;it was &amp;quot;stop trusting augmented_math as the backbone of the dataset.&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At that point I stopped trying to rescue the 30k augmented-heavy recipe. If the dataset takes repeated repair passes and still produces obvious bad rows in random review, then it stops being a clean experimental input and becomes the experiment itself.&lt;/p&gt;
&lt;p&gt;I did not want the final write-up to be “attention-only LoRA versus all-layer LoRA, but also maybe the augmented data had hidden garbage.” That would have made every downstream result harder to trust.&lt;/p&gt;
&lt;h3 id=&quot;2026-04-20-to-2026-04-21-i-rebuilt-around-original-rows-only&quot;&gt;2026-04-20 to 2026-04-21: I rebuilt around original rows only&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;OpenMathInstruct-2 train_1M&lt;/code&gt; had &lt;code&gt;29,468&lt;/code&gt; original-source rows across &lt;code&gt;gsm8k&lt;/code&gt; and &lt;code&gt;math&lt;/code&gt;. After the strict gate, &lt;code&gt;28,166&lt;/code&gt; survived cleanly enough to build a new candidate.&lt;/p&gt;
&lt;p&gt;The accepted source counts were:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;gsm8k&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 14618&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;math&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 13548&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The original-only dataset was smaller than the 30k target, but the quality profile was much better. It cleared the automatic checks that the augmented branch kept failing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;boxed-match rate, train: &lt;code&gt;1.0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;boxed-match rate, validation: &lt;code&gt;1.0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;suspicious rows, train: &lt;code&gt;0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;suspicious rows, validation: &lt;code&gt;0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;prompt-wrapper contamination hits, train: &lt;code&gt;0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;prompt-wrapper contamination hits, validation: &lt;code&gt;0&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I froze this path as &lt;code&gt;openmath_original_clean&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After a later split repair, the final retained manifest became:&lt;/p&gt;























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;split&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;&lt;code&gt;gsm8k&lt;/code&gt; source rows&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;&lt;code&gt;math&lt;/code&gt; source rows&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;total&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;train&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;13,145&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;12,203&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;25,348&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;validation&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1,473&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1,345&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2,818&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The old &lt;code&gt;openmath_30k*&lt;/code&gt; lineage was retired after that. I wanted one clear dataset path in the repo, not a dozen stale artifact branches that future-me or another agent might accidentally treat as active.&lt;/p&gt;
&lt;h3 id=&quot;2026-04-21-i-kept-the-system-prompt&quot;&gt;2026-04-21: I kept the system prompt&lt;/h3&gt;
&lt;p&gt;The render sanity check compared the frozen dataset with and without the system prompt under the actual &lt;code&gt;Qwen/Qwen3-8B&lt;/code&gt; chat template. The prompt added a constant &lt;code&gt;27&lt;/code&gt; tokens per example.&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;full_mean_with_system&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 340.25&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;full_mean_without_system&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 313.25&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;full_mean_delta&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 27.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That overhead was small enough to keep. More importantly, the prompt made the output contract explicit:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;You are a careful math solver. Solve the problem step by step. Put the final answer in \boxed{}.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I kept it because the raw problems themselves do not always carry the boxed-answer instruction. I also wanted training and evaluation to mirror each other as closely as possible.&lt;/p&gt;
&lt;h2 id=&quot;3-contracts-and-sanity-checks&quot;&gt;3. Contracts And Sanity Checks&lt;/h2&gt;
&lt;h3 id=&quot;2026-04-22-i-stopped-relying-on-memory&quot;&gt;2026-04-22: I stopped relying on memory&lt;/h3&gt;
&lt;p&gt;Once the dataset path was stable, the next risk was format drift. It is easy to train with one prompt, evaluate with another, parse answers a third way, and then accidentally report a prompt experiment as a model experiment.&lt;/p&gt;
&lt;p&gt;So I wrote the contracts down before the final result existed.&lt;/p&gt;
&lt;p&gt;I also had to clean up the planning docs. Some TODO labels were too compressed. A label like “contamination check” made sense to me while I was in the middle of the work, but it hid the actual operation: compare training-side &lt;code&gt;gsm8k&lt;/code&gt; problem text against held-out GSM8K test questions after canonical normalization, then block training if overlap is greater than zero.&lt;/p&gt;
&lt;p&gt;So I added an execution-clarity rule for the project: open tasks needed to say what the action was, why it mattered, what counted as done, and what happened if it failed. It sounds procedural, but it kept the later runbook from turning into shorthand only I could decode.&lt;/p&gt;
&lt;p&gt;The evaluation contract froze:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the same system prompt as training&lt;/li&gt;
&lt;li&gt;greedy decoding with &lt;code&gt;temperature = 0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;enable_thinking=False&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_new_tokens = 512&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;no custom stop tokens&lt;/li&gt;
&lt;li&gt;boxed-answer extraction only&lt;/li&gt;
&lt;li&gt;exact match after normalization&lt;/li&gt;
&lt;li&gt;the contamination report path&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The contamination check was a gate, not a note. If training-side &lt;code&gt;gsm8k&lt;/code&gt; rows overlapped the GSM8K test questions, the benchmark had to change or the dataset had to be rebuilt.&lt;/p&gt;
&lt;p&gt;The result schema was frozen around retained JSONL output under &lt;code&gt;artifacts/results/&amp;lt;run_id&amp;gt;/&lt;/code&gt;. It also kept &lt;code&gt;token_count&lt;/code&gt; and &lt;code&gt;cost&lt;/code&gt; in the canonical schema even when those values were &lt;code&gt;null&lt;/code&gt;, because I did not want the schema to change later just because telemetry became available.&lt;/p&gt;
&lt;p&gt;I also standardized on the word &lt;code&gt;condition&lt;/code&gt; for the comparison label, which sounds minor but made later artifacts easier to read:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;condition&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;attention_only&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;seed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;step&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 3169&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;2026-05-03-the-dataset-split-still-had-a-hidden-problem&quot;&gt;2026-05-03: the dataset split still had a hidden problem&lt;/h3&gt;
&lt;p&gt;When I ran the dataset-integrity gate, the benchmark side passed. The training-side &lt;code&gt;gsm8k&lt;/code&gt; rows had &lt;code&gt;0&lt;/code&gt; overlap with the &lt;code&gt;openai/gsm8k&lt;/code&gt; test split.&lt;/p&gt;
&lt;p&gt;But the local train/validation split failed a different check. OpenMath can include multiple accepted solutions for the same underlying problem. The original builder had split rows independently, so variants of the same problem could land on both sides.&lt;/p&gt;
&lt;p&gt;That would make validation loss too friendly. It would also make checkpoint or LR decisions look cleaner than they were.&lt;/p&gt;
&lt;p&gt;The fix was to group by canonical problem text before splitting. The retained report after the rebuild says:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;status&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#40A02B;--shiki-dark:#A6E3A1&quot;&gt; &amp;quot;pass&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;overlap_count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;train_val_overlap&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;row_id_overlap_count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;problem_text_overlap_count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;overlapped_val_row_count&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The final contamination report compared &lt;code&gt;13,145&lt;/code&gt; training rows with &lt;code&gt;source == &amp;quot;gsm8k&amp;quot;&lt;/code&gt; against &lt;code&gt;1,319&lt;/code&gt; GSM8K test questions. It found &lt;code&gt;overlap_count = 0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The raw files were not enough on their own, and the split rule mattered.&lt;/p&gt;
&lt;h3 id=&quot;2026-05-04-the-dataset-went-to-hugging-face&quot;&gt;2026-05-04: the dataset went to Hugging Face&lt;/h3&gt;
&lt;p&gt;After the repaired split, the frozen dataset payloads were published to &lt;code&gt;sumitdotml/lora-and-friends-dataset&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The local repo kept scripts, manifests, checksums, and audit evidence. The Hub repo carried the payloads:&lt;/p&gt;






























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;payload&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;rows&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;bytes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;raw train&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;25,348&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;25,528,287&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;raw validation&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2,818&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2,787,397&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;rendered train&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;25,348&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;29,989,535&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;rendered validation&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2,818&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;3,283,365&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;At this point I had a real dataset, a render path, an evaluation contract, and a contamination gate. I still did not know whether the training path would work.&lt;/p&gt;
&lt;h2 id=&quot;4-first-tinker-check&quot;&gt;4. First Tinker Check&lt;/h2&gt;
&lt;h3 id=&quot;2026-05-04-the-first-backend-check-caught-a-renderer-trap&quot;&gt;2026-05-04: the first backend check caught a renderer trap&lt;/h3&gt;
&lt;p&gt;The first Tinker smoke pass was supposed to be boring. It was one of the smallest checks in the whole project: can Tinker accept &lt;code&gt;Qwen/Qwen3-8B&lt;/code&gt;, rank &lt;code&gt;8&lt;/code&gt;, and the two LoRA condition switches?&lt;/p&gt;
&lt;p&gt;The Tinker cookbook &lt;code&gt;qwen3_disable_thinking&lt;/code&gt; renderer was correct for generation prompts, but its supervised-training path did not match the frozen SFT render contract. The project contract was the Hugging Face chat template with &lt;code&gt;enable_thinking=False&lt;/code&gt;, which renders an empty thinking block before the assistant answer:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;think&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&amp;lt;/think&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I changed the smoke runner so Tinker datums came from &lt;code&gt;AutoTokenizer.apply_chat_template(..., enable_thinking=False)&lt;/code&gt;, then masked loss only after the rendered prompt prefix.&lt;/p&gt;
&lt;p&gt;The retained smoke pass then gave me enough to lock the adapter defaults:&lt;/p&gt;























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;condition&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;train rows in smoke&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;optimizer steps&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;validation NLL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1.5048651695251465&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;8&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1.4733978509902954&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Tinker accepted:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;json&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;attention_only&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;train_attn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;train_mlp&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;train_unembed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;all_layer&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;train_attn&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;train_mlp&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;		&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#1E66F5;--shiki-dark:#89B4FA&quot;&gt;train_unembed&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;--shiki-light:#FE640B;--shiki-dark:#FAB387&quot;&gt; false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;	}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#7C7F93;--shiki-dark:#9399B2&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The public Tinker API did not expose local &lt;code&gt;lora_alpha&lt;/code&gt; or &lt;code&gt;lora_dropout&lt;/code&gt; fields through this training path. I recorded those as backend-owned instead of pretending the runner controlled them.&lt;/p&gt;
&lt;p&gt;There was one more small backend detail in the log: the first smoke attempt printed that the Tinker SDK version was outdated. I upgraded to &lt;code&gt;tinker==0.18.2&lt;/code&gt;, reran the smoke pass, and the warning did not reappear. Backend version drift becomes hard to reconstruct later, so I kept the note.&lt;/p&gt;
&lt;p&gt;The smoke pass also created checkpoint paths with a seven-day TTL. That did not matter for the final result yet, but it foreshadowed the export urgency after the main sweep.&lt;/p&gt;
&lt;h3 id=&quot;2026-05-05-i-ran-the-untouched-baseline-before-touching-lora&quot;&gt;2026-05-05: I ran the untouched baseline before touching LoRA&lt;/h3&gt;
&lt;p&gt;The baseline was &lt;code&gt;Qwen/Qwen3-8B&lt;/code&gt; on &lt;code&gt;openai/gsm8k&lt;/code&gt;, config &lt;code&gt;main&lt;/code&gt;, split &lt;code&gt;test&lt;/code&gt;, with &lt;code&gt;enable_thinking=false&lt;/code&gt;, &lt;code&gt;temperature=0&lt;/code&gt;, and &lt;code&gt;max_new_tokens=512&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The retained baseline run was:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;artifacts/results/baseline-qwen3-8b-gsm8k-001/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It scored:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;examples: &lt;code&gt;1,319&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;correct: &lt;code&gt;1,115&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;accuracy: &lt;code&gt;0.8453373768006065&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;extraction failures: &lt;code&gt;31&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;total eval tokens: &lt;code&gt;505,694&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I wanted this number before LR selection and before the main comparison. Otherwise, the final LoRA scores would float without a reference.&lt;/p&gt;
&lt;h2 id=&quot;5-learning-rate-and-batch-shape&quot;&gt;5. Learning Rate And Batch Shape&lt;/h2&gt;
&lt;h3 id=&quot;2026-05-06-the-lr-selection-plan-was-too-slow&quot;&gt;2026-05-06: the LR-selection plan was too slow&lt;/h3&gt;
&lt;p&gt;The first LR-selection protocol used &lt;code&gt;5,000&lt;/code&gt; train rows and &lt;code&gt;500&lt;/code&gt; validation rows. On paper it was reasonable. In practice it projected to roughly &lt;code&gt;20&lt;/code&gt; hours for the six-run sweep on Tinker.&lt;/p&gt;
&lt;p&gt;I rescaled it to a deterministic slice:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;first &lt;code&gt;512&lt;/code&gt; train rows&lt;/li&gt;
&lt;li&gt;first &lt;code&gt;128&lt;/code&gt; validation rows&lt;/li&gt;
&lt;li&gt;seed &lt;code&gt;7&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;LR grid: &lt;code&gt;1e-4&lt;/code&gt;, &lt;code&gt;3e-4&lt;/code&gt;, &lt;code&gt;1e-3&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;two conditions&lt;/li&gt;
&lt;li&gt;one epoch&lt;/li&gt;
&lt;li&gt;effective batch size &lt;code&gt;8&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The narrow purpose was to choose a peak LR per condition, not to run a final benchmark or a condition comparison.&lt;/p&gt;
&lt;p&gt;The selected LR was &lt;code&gt;3e-4&lt;/code&gt; for both:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;condition&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;LR&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;best validation NLL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;&lt;code&gt;1e-4&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3786645046540731&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;&lt;code&gt;3e-4&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3632619345728878&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;&lt;code&gt;1e-3&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3644437038722405&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;&lt;code&gt;1e-4&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3648794147648033&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;&lt;code&gt;3e-4&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3559855057286731&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;&lt;code&gt;1e-3&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.37397296784836664&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;I kept seed &lt;code&gt;7&lt;/code&gt; reserved for selection work so the main comparison seeds could be &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt;, and &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The LR-selection runner also caught a plain implementation bug before the expensive runs. I had assumed Tinker cookbook datum weights were raw torch tensors. They were &lt;code&gt;TensorData&lt;/code&gt; objects. The live one-step probe failed, the runner got fixed, and that mistake went into &lt;code&gt;AGENT_MISTAKES.md&lt;/code&gt;. The final result depends on the boring part where a runner can survive a one-step live probe before it is trusted for a sweep.&lt;/p&gt;
&lt;p&gt;I also split the training helper code into smaller modules:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;training/common.py&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;training/sft.py&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;training/lora.py&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The runnable scripts stayed separate: one for the smoke pass, one for LR selection, and later one for the main training sweep. This kept the orchestration scripts readable enough that I could still inspect what was being frozen.&lt;/p&gt;
&lt;h3 id=&quot;2026-05-07-speed-was-tempting-but-batch-size-was-not-free&quot;&gt;2026-05-07: speed was tempting, but batch size was not free&lt;/h3&gt;
&lt;p&gt;The initial runner shape used eight one-datum train requests before each optimizer step. A throughput probe showed that request shape mattered a lot at the same effective batch size:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/_astro/fig_08_throughput_probe.CqeELwHK_1u0NeL.webp&quot; alt=&quot;Throughput by request shape&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; fetchpriority=&quot;auto&quot; width=&quot;1851&quot; height=&quot;1100&quot;&gt;&lt;/p&gt;





















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;request shape&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;seconds per optimizer step&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;single_datum_calls&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;20.22187466151081&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;batched_datums&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;5.201307859155349&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;batched_datums_pipelined&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2.4104866901249693&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The fast version, &lt;code&gt;batched_datums_pipelined&lt;/code&gt;, submits one batched &lt;code&gt;forward_backward_async(...)&lt;/code&gt; request and queues the optimizer request before waiting. That kept the nominal effective batch size at &lt;code&gt;8&lt;/code&gt;; it only changed how the same batch was sent to Tinker.&lt;/p&gt;
&lt;p&gt;Then larger batches looked tempting. Batch &lt;code&gt;512&lt;/code&gt; and &lt;code&gt;1024&lt;/code&gt; were much faster in throughput probes. But batch size is a real hyperparameter for LoRA, so I made the larger-batch path earn its way in by validation NLL, and it did not.&lt;/p&gt;


























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;condition&lt;/th&gt;&lt;th&gt;selected batch/LR&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;selected NLL&lt;/th&gt;&lt;th&gt;best larger-batch candidate&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;candidate NLL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td&gt;batch 8, &lt;code&gt;3e-4&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3632619345728878&lt;/td&gt;&lt;td&gt;batch 512, &lt;code&gt;1e-3&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.37616809419132946&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td&gt;batch 8, &lt;code&gt;3e-4&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3559855057286731&lt;/td&gt;&lt;td&gt;batch 512, &lt;code&gt;1e-3&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.3569802998485914&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;So the main run kept effective batch size &lt;code&gt;8&lt;/code&gt;, used the pipelined request shape, and kept the final partial batch of &lt;code&gt;4&lt;/code&gt; rows rather than wrapping around and duplicating examples. The fast path was real, but it was only allowed to change operations, not silently change the scientific comparison.&lt;/p&gt;
&lt;h2 id=&quot;6-the-six-runs&quot;&gt;6. The Six Runs&lt;/h2&gt;
&lt;h3 id=&quot;2026-05-08-main-001-started&quot;&gt;2026-05-08: &lt;code&gt;main-001&lt;/code&gt; started&lt;/h3&gt;
&lt;p&gt;The main run expanded to six sequential condition/seed runs:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;main-001-attention_only-seed-0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main-001-attention_only-seed-1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main-001-attention_only-seed-2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main-001-all_layer-seed-0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main-001-all_layer-seed-1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main-001-all_layer-seed-2&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The launch command was:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;uv run training/run_main_training.py --run-prefix main-001&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each run used:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;train rows: &lt;code&gt;25,348&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;validation rows: &lt;code&gt;2,818&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;epochs: &lt;code&gt;2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;effective batch size: &lt;code&gt;8&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;optimizer steps per epoch: &lt;code&gt;3,169&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;total optimizer steps: &lt;code&gt;6,338&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;warmup steps: &lt;code&gt;190&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;peak LR: &lt;code&gt;3e-4&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;min LR: &lt;code&gt;3e-5&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The validation/checkpoint cadence was &lt;code&gt;1000&lt;/code&gt;, &lt;code&gt;2000&lt;/code&gt;, &lt;code&gt;3169&lt;/code&gt;, &lt;code&gt;4000&lt;/code&gt;, &lt;code&gt;5000&lt;/code&gt;, &lt;code&gt;6000&lt;/code&gt;, and &lt;code&gt;6338&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I monitored this like a long-running job, because it was one. The raw log has many repeated entries that look boring at first glance:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;validation step=1000 nll=...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;validation step=2000 nll=...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;validation step=3169 nll=...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Those entries showed that the run was still alive, that validation/checkpoint blocks resumed correctly, and that every retained checkpoint path was read from the active run rather than guessed from a sibling run.&lt;/p&gt;
&lt;p&gt;I had already made that mistake once while logging a checkpoint URL by analogy from a sibling run. The fix was simple: read the exact checkpoint string from the active run’s &lt;code&gt;metrics.jsonl&lt;/code&gt; or summary before writing it down. After that, I treated checkpoint paths as evidence, not as strings I could reconstruct from memory.&lt;/p&gt;
&lt;h3 id=&quot;step-3169-kept-showing-up&quot;&gt;Step 3169 kept showing up&lt;/h3&gt;
&lt;p&gt;The first attention-only seed hit:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;main-001-attention_only-seed-0: validation step=3169 nll=0.336164&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Its final summary selected step &lt;code&gt;3169&lt;/code&gt;, not the final step &lt;code&gt;6338&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;selected best validation step: 3169&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;selected validation_mean_nll: 0.33616363178874076&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then seed &lt;code&gt;1&lt;/code&gt; selected step &lt;code&gt;3169&lt;/code&gt;, then seed &lt;code&gt;2&lt;/code&gt;, then all-layer seed &lt;code&gt;0&lt;/code&gt;, then all-layer seed &lt;code&gt;1&lt;/code&gt;, then all-layer seed &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;At the end, every run selected the one-epoch boundary:&lt;/p&gt;








































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;run&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;selected step&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;validation NLL&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;main-001-attention_only-seed-0&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;3169&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.33616363178874076&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;main-001-attention_only-seed-1&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;3169&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.33621202263413363&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;main-001-attention_only-seed-2&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;3169&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.33651007850933906&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;main-001-all_layer-seed-0&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;3169&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.33611938013674597&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;main-001-all_layer-seed-1&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;3169&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.33655583715915666&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;main-001-all_layer-seed-2&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;3169&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.33634131648081267&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;/_astro/fig_05_validation_nll_diagnostic.pUHefRY0_RBoaC.webp&quot; alt=&quot;Validation NLL by training step&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; fetchpriority=&quot;auto&quot; width=&quot;1911&quot; height=&quot;1158&quot;&gt;&lt;/p&gt;
&lt;p&gt;The selected NLL means were close:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;attention_only&lt;/code&gt;: &lt;code&gt;0.3362952443107378&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;all_layer&lt;/code&gt;: &lt;code&gt;0.3363388445922384&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But the post-3169 behavior was different. Attention-only stayed near the selected NLL. All-layer jumped more sharply after the one-epoch boundary; at step &lt;code&gt;4000&lt;/code&gt;, all-layer mean NLL was &lt;code&gt;0.3457733619534809&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I did not treat that as the final result though; it was a training diagnostic, and the actual comparison still had to come from GSM8K.&lt;/p&gt;
&lt;p&gt;For all-layer seed &lt;code&gt;1&lt;/code&gt;, the NLL moved from &lt;code&gt;0.33655583715915666&lt;/code&gt; at step &lt;code&gt;3169&lt;/code&gt; to &lt;code&gt;0.34549824556309505&lt;/code&gt; at step &lt;code&gt;4000&lt;/code&gt;. For all-layer seed &lt;code&gt;2&lt;/code&gt;, it moved from &lt;code&gt;0.33634131648081267&lt;/code&gt; to &lt;code&gt;0.3461580484910246&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The lowest-NLL checkpoint rule prevented later checkpoints from becoming attractive simply because they were later or because the run had continued longer.&lt;/p&gt;
&lt;h3 id=&quot;the-sweep-finished-with-six-pass-summaries&quot;&gt;The sweep finished with six pass summaries&lt;/h3&gt;
&lt;p&gt;The final all-layer seed completed on 2026-05-09. The log entry was satisfying because the sweep had finally become a finite object:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;completion artifacts now exist in artifacts/results/main-001-all_layer-seed-2/:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;manifest.json, metrics.jsonl, sample_render.txt, summary.json&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each of the six selected checkpoints had a seven-day Tinker TTL, so the next step was urgent enough: export the selected checkpoint states into something that could be evaluated and published.&lt;/p&gt;
&lt;h2 id=&quot;7-checkpoint-export&quot;&gt;7. Checkpoint Export&lt;/h2&gt;
&lt;h3 id=&quot;2026-05-09-training-weights-were-not-sampler-weights&quot;&gt;2026-05-09: training weights were not sampler weights&lt;/h3&gt;
&lt;p&gt;The selected training checkpoints used &lt;code&gt;weights/...&lt;/code&gt; paths. I initially wanted to use those directly for publication/evaluation, but the sampling client rejected that shape.&lt;/p&gt;
&lt;p&gt;The actual server response was:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tinker.BadRequestError: Error code: 400 - {&amp;#39;detail&amp;#39;: &amp;#39;model_path must point to a sampler_weights checkpoint, got weights&amp;#39;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So the selected training state checkpoints had to be converted to sampler-format weights before any evaluation could run.&lt;/p&gt;
&lt;p&gt;The working export path was:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;load_state_async(training_checkpoint_path)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;save_weights_for_sampler_async(export_name)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The six sampler checkpoint URIs then looked like:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tinker://.../sampler_weights/export-main-001-attention_only-seed-0-step-3169&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;tinker://.../sampler_weights/export-main-001-all_layer-seed-2-step-3169&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After that, the converted adapters were uploaded to the Hugging Face model repo. The published layout is:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;checkpoints/best-checkpoints/attention_only/seed-{0,1,2}/step-3169/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;checkpoints/best-checkpoints/all_layer/seed-{0,1,2}/step-3169/&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The sampler weights also made the adapter-size difference concrete. The attention-only sampler exports were about &lt;code&gt;30.8M&lt;/code&gt; bytes each in the Tinker checkpoint listing, while the all-layer sampler exports were about &lt;code&gt;87.5M&lt;/code&gt; bytes each. That was expected from the larger adapter scope, but it was useful to see it in the retained checkpoint metadata rather than only as intuition.&lt;/p&gt;
&lt;h2 id=&quot;8-gsm8k-evaluation&quot;&gt;8. GSM8K Evaluation&lt;/h2&gt;
&lt;h3 id=&quot;2026-05-10-the-evaluation-runner-needed-checkpoint-awareness&quot;&gt;2026-05-10: the evaluation runner needed checkpoint awareness&lt;/h3&gt;
&lt;p&gt;The baseline path already existed, but checkpoint evaluation needed more safeguards. I added support for:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;--checkpoint-path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;explicit &lt;code&gt;--seed&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;explicit &lt;code&gt;--condition&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;a run-id slug that would not collide when checkpoint paths ended similarly&lt;/li&gt;
&lt;li&gt;step parsing only from literal &lt;code&gt;step-&amp;lt;digits&amp;gt;&lt;/code&gt; tokens&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also made the argument validation fail fast if a checkpoint path was passed without condition or seed. I did not want a LoRA checkpoint quietly defaulting to &lt;code&gt;base&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Before launching the full sweep, I ran a one-example probe with &lt;code&gt;attention_only&lt;/code&gt; seed &lt;code&gt;0&lt;/code&gt;. It returned &lt;code&gt;1/1&lt;/code&gt; correct and confirmed the full path:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;sampler URI -&amp;gt; tokenizer -&amp;gt; Tinker sample -&amp;gt; boxed-answer extraction -&amp;gt; scoring -&amp;gt; retained artifacts&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;the-six-eval-runs&quot;&gt;The six eval runs&lt;/h3&gt;
&lt;p&gt;The full sweep ran at &lt;code&gt;--concurrency 16&lt;/code&gt;, no limit, against the six selected sampler checkpoints.&lt;/p&gt;
&lt;p&gt;The baseline stayed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;accuracy: &lt;code&gt;0.8453373768006065&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;correct: &lt;code&gt;1115/1319&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;extraction failures: &lt;code&gt;31&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The six LoRA eval runs were:&lt;/p&gt;






















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;condition&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;seed&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;accuracy&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;correct / total&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;extraction failures&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9044730856709629&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1193 / 1319&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;6&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9067475360121304&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1196 / 1319&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9052312357846853&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1194 / 1319&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;5&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.8991660348749052&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1186 / 1319&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;5&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9021986353297953&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1190 / 1319&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;2&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9014404852160728&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1189 / 1319&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;8&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Aggregated by condition:&lt;/p&gt;


























&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;condition&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;mean accuracy&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;min&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;max&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;range&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;attention_only&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9054839524892596&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9044730856709629&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9067475360121304&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.0022744503411676&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;code&gt;all_layer&lt;/code&gt;&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9009350518069245&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.8991660348749052&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.9021986353297953&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;0.0030326004548901&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;img src=&quot;/_astro/fig_03_gsm8k_accuracy_chart.CjTnoPdB_Zy09gH.webp&quot; alt=&quot;GSM8K accuracy by condition&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; fetchpriority=&quot;auto&quot; width=&quot;1820&quot; height=&quot;1187&quot;&gt;&lt;/p&gt;
&lt;p&gt;The mean gap was &lt;code&gt;0.0045489006823352&lt;/code&gt;, or about &lt;code&gt;0.455&lt;/code&gt; percentage points, in favor of attention-only. Under the frozen rule, that is below the &lt;code&gt;0.01&lt;/code&gt; threshold for a winner claim. So the honest reading is local: attention-only ended higher in this run set and used a smaller adapter, but the result stays inside the pre-declared inconclusive band.&lt;/p&gt;
&lt;h3 id=&quot;the-disagreement-view-made-the-result-easier-to-read&quot;&gt;The disagreement view made the result easier to read&lt;/h3&gt;
&lt;p&gt;The net accuracy chart showed the final result, but the disagreement chart explained its shape better.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/_astro/fig_04_paired_seed_slope.ClcugjqE_Z1OFEzw.webp&quot; alt=&quot;Per-seed prediction disagreement on GSM8K&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; fetchpriority=&quot;auto&quot; width=&quot;2511&quot; height=&quot;1253&quot;&gt;&lt;/p&gt;
&lt;p&gt;For paired seeds:&lt;/p&gt;





































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align:right&quot;&gt;seed&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;attention-only only&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;all-layer only&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;both correct&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;both wrong&lt;/th&gt;&lt;th style=&quot;text-align:right&quot;&gt;delta&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align:right&quot;&gt;0&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;54&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;47&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1139&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;79&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;+7&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:right&quot;&gt;1&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;49&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;43&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1147&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;80&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;+6&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style=&quot;text-align:right&quot;&gt;2&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;46&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;41&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;1148&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;84&lt;/td&gt;&lt;td style=&quot;text-align:right&quot;&gt;+5&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The two conditions mostly agree, with the difference coming from a narrow band of examples where attention-only is correct slightly more often than the reverse.&lt;/p&gt;
&lt;h2 id=&quot;9-figures-and-cleanup&quot;&gt;9. Figures And Cleanup&lt;/h2&gt;
&lt;h3 id=&quot;2026-05-10-the-figure-pass-changed-how-i-told-the-result&quot;&gt;2026-05-10: the figure pass changed how I told the result&lt;/h3&gt;
&lt;p&gt;The figure pipeline produced eight retained artifacts. This worklog uses four of them:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fig_03&lt;/code&gt;: GSM8K accuracy by condition&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fig_04&lt;/code&gt;: per-seed prediction disagreement&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fig_05&lt;/code&gt;: validation NLL by training step&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fig_08&lt;/code&gt;: throughput by request shape&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The figure pass forced me to separate claims that were getting blurred together:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;validation NLL explains checkpoint selection&lt;/li&gt;
&lt;li&gt;GSM8K accuracy explains benchmark performance&lt;/li&gt;
&lt;li&gt;prediction disagreement explains the narrow accuracy gap&lt;/li&gt;
&lt;li&gt;throughput explains an operational runner choice&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The fig_04 design took the most iteration. The original idea was a slope/delta view showing net correct-count deltas of &lt;code&gt;+7&lt;/code&gt;, &lt;code&gt;+6&lt;/code&gt;, and &lt;code&gt;+5&lt;/code&gt;. It kept reading like a progression over time or a tiny effect exaggerated by the axis.&lt;/p&gt;
&lt;p&gt;The raw log records several rejected forms: vertical slope, leader labels, corner text boxes, horizontal dumbbell, vertical paired columns, and delta-only bars. The recurring problem was that the visual form was louder than the claim. A &lt;code&gt;0.4&lt;/code&gt; to &lt;code&gt;0.5&lt;/code&gt; percentage-point gap should not be drawn like a dramatic swing.&lt;/p&gt;
&lt;p&gt;The final chart became per-seed disagreement small multiples. That matched the actual question better: where do the two conditions disagree?&lt;/p&gt;
&lt;p&gt;The log records the final contingency numbers before implementation:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes catppuccin-latte catppuccin-mocha&quot; style=&quot;--shiki-light:#4c4f69;--shiki-dark:#cdd6f4;--shiki-light-bg:#eff1f5;--shiki-dark-bg:#1e1e2e;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;seed 0: attention_only_only=54, all_layer_only=47, both_correct=1139, both_wrong=79&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;seed 1: attention_only_only=49, all_layer_only=43, both_correct=1147, both_wrong=80&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;seed 2: attention_only_only=46, all_layer_only=41, both_correct=1148, both_wrong=84&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I kept the filename as &lt;code&gt;fig_04_paired_seed_slope&lt;/code&gt; so existing artifact paths did not move, but the chart itself became the disagreement view.&lt;/p&gt;
&lt;h3 id=&quot;the-cleanup-rule&quot;&gt;The cleanup rule&lt;/h3&gt;
&lt;p&gt;The result can say:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;both LoRA conditions improved over the untouched baseline on GSM8K&lt;/li&gt;
&lt;li&gt;attention-only had the higher three-seed mean in this retained run set&lt;/li&gt;
&lt;li&gt;attention-only had a small paired-seed disagreement edge&lt;/li&gt;
&lt;li&gt;the gap is below the frozen &lt;code&gt;0.01&lt;/code&gt; threshold for a winner claim&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result cannot say:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;attention-only LoRA is generally better&lt;/li&gt;
&lt;li&gt;all-layer LoRA is bad&lt;/li&gt;
&lt;li&gt;the same result should hold across other ranks, models, datasets, or benchmarks&lt;/li&gt;
&lt;li&gt;validation NLL explains the benchmark gap as a mechanism&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;10-what-this-leaves-me-with&quot;&gt;10. What This Leaves Me With&lt;/h2&gt;
&lt;p&gt;The final comparison was the easy sentence at the end of a lot of setup.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I started too broad and had to cut the project down to one comparison.&lt;/li&gt;
&lt;li&gt;I thought the first dataset recipe was close, then the audits kept proving otherwise.&lt;/li&gt;
&lt;li&gt;I abandoned the augmented-heavy branch because it made the dataset itself the unstable variable.&lt;/li&gt;
&lt;li&gt;I kept the system prompt because the output contract needed to be explicit.&lt;/li&gt;
&lt;li&gt;I froze the eval contract before the result existed.&lt;/li&gt;
&lt;li&gt;I had to repair train/validation leakage by grouping canonical problem text.&lt;/li&gt;
&lt;li&gt;The smoke pass caught a render mismatch before paid runs.&lt;/li&gt;
&lt;li&gt;LR selection picked &lt;code&gt;3e-4&lt;/code&gt; for both conditions.&lt;/li&gt;
&lt;li&gt;Throughput work mattered, but only after batch size stayed controlled.&lt;/li&gt;
&lt;li&gt;All six main runs selected step &lt;code&gt;3169&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Training checkpoints had to be converted to sampler weights before evaluation.&lt;/li&gt;
&lt;li&gt;The disagreement chart was more honest than only talking about a mean gap.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The result is small, but the path to making it trustworthy was not.&lt;/p&gt;
&lt;p&gt;If I were doing the next version, I would keep the same discipline: narrow the question first, freeze contracts before seeing benchmark numbers, treat dataset audits as part of the experiment, and make every convenience change prove that it does not change the comparison. The follow-up question I would carry forward is when a narrower adapter is enough, and when broader adaptation is worth the extra size.&lt;/p&gt;</content:encoded></item></channel></rss>