<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Paulius Rauba]]></title><description><![CDATA[PhD Machine Learning @ Cambridge | Lecturer @ ISM University of Management and Economics]]></description><link>https://www.pauliusrauba.com</link><image><url>https://substackcdn.com/image/fetch/$s_!MwsT!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa4d1fba9-2739-4c95-bdac-7aabcabab07b_1365x1365.jpeg</url><title>Paulius Rauba</title><link>https://www.pauliusrauba.com</link></image><generator>Substack</generator><lastBuildDate>Thu, 09 Apr 2026 19:40:50 GMT</lastBuildDate><atom:link href="https://www.pauliusrauba.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Paulius Rauba]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[pauliusrauba@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[pauliusrauba@substack.com]]></itunes:email><itunes:name><![CDATA[Paulius Rauba]]></itunes:name></itunes:owner><itunes:author><![CDATA[Paulius Rauba]]></itunes:author><googleplay:owner><![CDATA[pauliusrauba@substack.com]]></googleplay:owner><googleplay:email><![CDATA[pauliusrauba@substack.com]]></googleplay:email><googleplay:author><![CDATA[Paulius Rauba]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Why we divide by the square root of the key dimension in attention ]]></title><description><![CDATA[There are three implicit assumptions we make]]></description><link>https://www.pauliusrauba.com/p/why-we-divide-by-the-square-root</link><guid isPermaLink="false">https://www.pauliusrauba.com/p/why-we-divide-by-the-square-root</guid><dc:creator><![CDATA[Paulius Rauba]]></dc:creator><pubDate>Sun, 12 Oct 2025 06:55:03 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/b1d20cee-e9c5-45e6-8b62-45a9f9bc22f4_250x187.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>There&#8217;s a really good reason why we divide by \(\sqrt{d_k}\). When you&#8217;re computing the dot product between two vectors, you&#8217;re performing two operations: multiplying the components <em>within each vector dimension</em>, and adding all of the multiplied terms up. Therefore, the more components your vector has, the more additions you will end up doing, and the larger (in magnitude) your result will be.</p><p>To make sense as to why dividing by \(\sqrt{d_k}\) helps, we have to understand <strong>three assumptions</strong> we make about attention at this stage. These are not guaranteed outcomes of the computation, these are <em>assumed by design.</em></p><ul><li><p><em>Assumption 1:</em> we assume that each individual entry in the vector (both query and key vectors) is centered around zero, on average. This means that if our query vector becomes longer (consider increasing the dimensionality of your vectors to make the models more expressive), the resulting dot product between your query and key vectors will also have a mean of zero, in expectation.</p></li><li><p><em>Assumption 2:</em> we assume that each individual entry has variance one. Of course, we cannot compute the variance of the individual entry, but we assume it&#8217;s drawn from a distribution of variance one (we do not make any assumptions about what distribution this is drawn from, however).</p></li><li><p><em>Assumption 3: </em>we assume different entries are independent. We need this to make assumptions about how the variance increases as the dimensionality of the vectors grow.</p></li></ul><p><em>How do these assumptions explain why we divide by \(\sqrt{d_k}\)?</em> When we compute a dot product between two vectors, we&#8217;re summing up the products of the corresponding entries. We want to ensure that this result is stable, so that its properties would not depend on the dimensionality of the vectors.</p><p>If we increase the dimensionality, the expected entry will still be zero, since multiplying random variables with a mean of zero will result in zero (<em>Assumption</em> 1). We do not need to do anything here.</p><p>If each entry in both the query and key have variance one (<em>Assumption</em> 2), and their entries are independent (<em>Assumption 3)</em>, then each individual product term will also have variance one. This comes from a <a href="https://math.stackexchange.com/questions/4271183/variance-of-product-of-two-random-variables-fx-y-xy?">property</a> in probability theory: when you multiply two independent random variables with mean zero and variance one, their product also has variance one.</p><p>But we are adding up more than one term. For a vector of length 100, we will add up 100 of such terms. This creates a challenge because the variance adds when you sum independent variables. When we sum 100 of such terms, our variance will be equal to 100, not 1. The reason this is problematic is because high variance scores might mean some scores will be very high or small by chance; which will, in turn, produce spiked probability distributions after applying softmax. This is fundamentally bad for learning and obtaining good gradients.</p><p>Dividing by \(\sqrt{d_k}\) solves this. This is because dividing by the standard deviation (which is exactly \(\sqrt{d_k}\) brings back the total variance to one. Therefore, regardless of the size of our query and key vectors, we can ensure the computed scores have both zero mean and variance one. While all three assumptions are necessary for this, and we enforce this by performing appropriate scaling operations, in practice, the query and key entries are not independent after training; and their marginals may not stay unit variance.</p><div><hr></div><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\n\\text{Attention}(Q, K, V) = \\text{softmax}\\!\\left(\\frac{QK^{T}}{\\sqrt{d_k}}\\right)V\n&quot;,&quot;id&quot;:&quot;UPUGULOCHN&quot;}" data-component-name="LatexBlockToDOM"></div><p></p>]]></content:encoded></item><item><title><![CDATA[Attention mechanism for non-Euclidean spaces]]></title><description><![CDATA[Looking at the geometric assumptions behind attention.]]></description><link>https://www.pauliusrauba.com/p/attention-mechanism-for-non-euclidean</link><guid isPermaLink="false">https://www.pauliusrauba.com/p/attention-mechanism-for-non-euclidean</guid><dc:creator><![CDATA[Paulius Rauba]]></dc:creator><pubDate>Sun, 05 Oct 2025 17:45:19 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!w-X2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Main takeaway: </strong>When attention is used on data that has a geometric interpretation&#8212; like warped images, spheres, 360&#176; video&#8212;the <em>ideal </em>operation is computing a continuous attention integral over the entire domain. In practice, this is not possible. We therefore compute a discrete approximation, i.e., discrete attention between different tokens. To do this correctly, we should take into account not only how tokens relate to each other <em>but also </em>how much area or volume each token represents.  The practical solution is simple: add correct weights (the area of the patch) to make the attention geometry-aware. </p><p>*<em><strong>Note for readers</strong>: Substack still has issues rendering LaTex. In case you do not see rendered equations, I&#8217;ve found reloading the substack directly in the browser helps.</em></p><div><hr></div><h2>1. Attention for non-Euclidean domains: why should we care?</h2><p>The reason we should care about attention for non-Euclidean spaces rests on three premises:</p><ul><li><p><em>Premise 1: </em>Many important tasks have intrinsic structure that is not &#8220;flat&#8221;. This includes graphs (e.g. modeling molecules), spheres (e.g. modeling weather) or other curved domains (e.g. 360&#176; videos).</p></li><li><p><em>Premise 2: </em>Standard attention in Transformers compares items using the dot product in a flat vector space and uses positional hacks to inject structure. </p></li><li><p><em>Premise 3: </em>In domains where intrinsic structure matters, positional hacks become either suboptimal or insufficient and we need to make the attention mechanism itself aware of the geometry.</p></li></ul><p>If you accept these premises, you should agree that understanding how to use attention in non-Euclidean domains can improve the performance on such structures. Given that there are many unsolved tasks in these domains, <em>and </em>that the attention mechanism has been the primary algorithmic driver behind the success of many deep learning methods, it warrants understanding how to generalize the attention mechanism to these curved domains.</p><p>This blog post is inspired by the great work by a team from NVIDIA that <a href="https://arxiv.org/pdf/2505.11157">published</a> a way to generalize attention on the sphere (and released CUDA kernels for neighborhood attention computation). Check the paper out!</p><p>In what follows, I&#8217;ll assume the example of modeling the sphere to motivate how attention can be generalized to non-Euclidean domains.</p><h2>2. Recap of attention</h2><p><strong>Setup. </strong>We have tokens \(x_1, \ldots, x_n \in \mathbb{R}^{d_{\text{model}}}\). Depending on the problem, our tokens might represent different things. In language modeling, each token is a sub-word unit that is represented by a long learned vector; in weather modeling, the token represents an area on the sphere with each channel being, for instance, a climatological measurement; in computer vision, each token represents a group of pixels and their learned embeddings.</p><p>For attention, we require learning three linear maps from the token to three other vectors. These three maps are learned and shared across all tokens:</p><ul><li><p><em>The query: </em>\(q(\cdot): \mathbb{R}^{d_{\text{model}}} \to \mathbb{R}^{d_{k}}\)</p></li><li><p><em>The key: </em>\(k(\cdot): \mathbb{R}^{d_{\text{model}}} \to \mathbb{R}^{d_{k}}\)</p></li><li><p><em>The value: </em>\(v(\cdot): \mathbb{R}^{d_{\text{model}}} \to \mathbb{R}^{d_{v}}\)</p></li></ul><p>Therefore, <em>each</em> token now has three vectors associated with it. Suppose you&#8217;re at token \(x\) and you&#8217;re interested in computing the attention scores between \(x\) and all other tokens. </p><p>Standard attention can be separated out into three conceptual steps. First, computing the score between the query of \(x\) and the key of another token. Second, computing the softmax weights between the query of \(x\) and all other keys. Third, take a weighted sum of the values using the softmax weights. In more detail:</p><p><strong>Step 1. </strong><em>Compute the dot product between the query of \(x\) and the key of another token.</em> </p><p>For our token \(x\) and a <em>single </em>other token \(x_j\), we obtain the score by computing their dot product and dividing by the square root of query/key dimensions to keep the variance the same:</p><p> \[s_j = \frac{q(x) \cdot k(x_j)}{\sqrt{d_k}}\].</p><p><strong>Step 2. </strong><em>Computing the weights between the query of \(x\) and all other keys.</em></p><p>We now need to convert our score into a probability distribution over the keys. This answers the question: &#8220;given this query, how much weight should I place on each key?&#8221; So, the score for key \(j\) becomes:</p><p>\[\alpha_j = \frac{\exp(s_j)}{\sum_{i=1}^n \exp(s_i)}.\]</p><p><em><strong>Step 3.</strong> Take a weighted sum of the values using the softmax weights.</em> The query computes a weighted sum of the values:</p><p>\[\text{Attn}(x) = \sum_{j=1}^n \alpha_j(x)\, v(x_j).\]</p><h2>3. Attention assumes each token covers the same area</h2><p>Vanilla attention uses a dot-product similarity in embedding space <em>and implicitly assumes each token covers the same area/volume. </em>If we are operating on a continuous space where tokens correspond to regions with area, the average is a modeling approximation to an integral (with respect to the domain&#8217;s measure). To do it perfectly, we would need to integrate over the area. However, this is almost always impossible and we therefore use a weighted sum, with a particular weight per token. </p><p>So, the standard attention formula relies on the scores \(\alpha_j\) and \(v(\cdot)\).</p><p>\[\text{Attn}(x) \approx \sum_{j=1}^n \alpha_j(x)\, v(x_j)\].</p><p>This becomes an approximation only if we are indeed approximating a continuous domain. We can explicitly include the size of the region of this area as follows: </p><p>\[\text{Attn}(x) \approx \sum_{j=1}^n \alpha_j(x)\, v(x_j)\, \underbrace{(\text{area of cell } j)}_{\text{Include this.}}\]</p><p>When tokens correspond to regions of a continuous domain, vanilla attention leaves out the &#8220;area of a cell&#8221; factor. This is alright in case the areas are the same. When areas differ&#8212;e.g. a sphere&#8212;we should include a weighting of that area. Here, \(\alpha_j\) represents similarity from the softmax and \(\omega_j\) accounts for the cell area. At this stage, we still have to renormalize attention (we have added area weights but have not divided by the normalization constant that makes the weights sum to 1). </p><p>To understand how to do this, let&#8217;s look at the sphere as an example.</p><h2>4. Size of an area and the sphere</h2><p>On a flat plane, dividing the space into equal squares means every cell has the same area. But the sphere is curved. Therefore, equal steps in latitude and longitude do not correspond to equal surface area. We typically divide the sphere into grids which are represented by intersections of latitude-longitude.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!w-X2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!w-X2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!w-X2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!w-X2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!w-X2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!w-X2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png" width="351" height="351" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/be9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1024,&quot;width&quot;:1024,&quot;resizeWidth&quot;:351,&quot;bytes&quot;:914860,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://pauliusrauba.substack.com/i/174814950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!w-X2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 424w, https://substackcdn.com/image/fetch/$s_!w-X2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 848w, https://substackcdn.com/image/fetch/$s_!w-X2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!w-X2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe9704a6-19d4-4438-9345-0d9db004e55b_1024x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A representation of a sphere with grids that form different surface areas from latitude and longitude lines intersecting.</figcaption></figure></div><p>Therefore, for our attention formula, we need to account for these differences in sizes. We can now ask: how should we weight each area according to its size?</p><p>The answer lies in computing the actual area of each grid cell. On a unit sphere, a small patch of area dA can be expressed as dA = (height) (width), where we parameterize position using two angles:</p><ul><li><p>\(\theta\) to denote how far down the North Pole we are (0 at the North Pole, \(\pi\) at the South Pole). This convention is referred to as <a href="https://en.wikipedia.org/wiki/Colatitude">colatitude</a>.</p></li><li><p>\(\phi\) to denote how far around the sphere&#8212;longitude. </p></li></ul><p>The area for a tiny area on a sphere is then </p><p>\[dA = \underbrace{d\theta}_{\text{height}}\underbrace{\sin\theta d\phi}_{\text{width}}\].</p><p><strong>Explaining the height. </strong>On a unit sphere, the colatitude angle \(\theta\) measures your angular position from the North Pole. For each meridian line (i.e. the longitude line), the radius \(\rho\) (depicted in green in the figure below) is 1. If we move along the same meridian line further away from the North Pole, we will have moved along the arc by a distance of radius \(\times d\theta\) which is just \(d\theta\)&#8212;obtained by applying the fundamental rule: arc-length = radius \(\times\) angle. Intuitively: if you have not moved, then your arc length is zero. If you&#8217;ve moved 90 degrees (\( \frac{\pi}{2}\) units), your arc length is \( \frac{\pi}{2} \approx 1.57\) units.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kTHY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kTHY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 424w, https://substackcdn.com/image/fetch/$s_!kTHY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 848w, https://substackcdn.com/image/fetch/$s_!kTHY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 1272w, https://substackcdn.com/image/fetch/$s_!kTHY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kTHY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png" width="456" height="456" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:720,&quot;width&quot;:720,&quot;resizeWidth&quot;:456,&quot;bytes&quot;:52867,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://pauliusrauba.substack.com/i/174814950?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!kTHY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 424w, https://substackcdn.com/image/fetch/$s_!kTHY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 848w, https://substackcdn.com/image/fetch/$s_!kTHY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 1272w, https://substackcdn.com/image/fetch/$s_!kTHY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fab3150a8-8bf4-4f6c-9b77-97a9c48a69d4_720x720.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A sphere where \(\theta\) denotes the angular position from the North Pole and \(\phi\) denotes the angular position around the sphere&#8217;s axis (longitude).</figcaption></figure></div><p><strong>Explaining the width. </strong>At colatitude \(\theta\), you&#8217;re standing on a small circle (a latitude line). The radius of this circle is \(\sin(\theta)\). When you move through a small longitude angle \(d\phi\), you trace an arc along this smaller circle. Therefore, the width will be \(\sin(\theta) \times d\phi\). </p><p>Combining the width and height together, we obtain \(dA = \sin\theta d\theta d\phi\).</p><h2>5. Attention performed on a sphere</h2><p>We will now use two facts we&#8217;ve built up:</p><ul><li><p>Standard attention implicitly assumes equal area/volume represented by each token.</p></li><li><p>We have computed a way to weigh the area of a specific token on a sphere.</p></li></ul><p>Now, let&#8217;s fix vanilla attention by incorporating the weighting scheme we have introduced. Recall the standard vanilla attention formula:</p><p>\[\text{Attn}(x) = \sum_{j=1}^n \alpha_j(x)\, v(x_j).\]</p><p>Then a faithful spherical average must be weighted by the area. If we introduce the area as \(\omega\), then our attention becomes:</p><p>\[\text{Attn}(x) \approx \sum_{j=1}^n \alpha_j(x)\, v(x_j) \omega_j.\]</p><p>That is, in effect, all there is to it.</p><p>We can still do a bit more to be very precise. Notice that both \(\alpha\) and \(\omega\) are encoding some sort of weighting: \(\alpha\) is encoding post-softmax weights and \(\omega\) is encoding quadrature weights. We can combine them such that there is a single set of probabilities that reflect, simultaneously, the importance and area of each cell. </p><p>Recall the attention weights are \[\alpha_j = \frac{\exp(s_j)}{\sum_{i=1}^n \exp(s_i)}.\] We can re-define the weights to mean both similarity and area, and then normalize by softmax. Two changes must then be made: (1) on the <em>numerator, </em>we should multiply by the weight of the cell with which we are computing attention to; (2) on the <em>denominator, </em>we need to ensure the weights still sum to 1 for it to form a valid probability distribution. Therefore, our new weights are:</p><p>\[\tilde{\alpha}_j = \frac{\exp(s_j)\omega_j}{\sum_i\exp(s_i)\omega_i}\]</p><p>There&#8217;s one final trick we can do to make the calculations easier. Recall the property that exponential terms can be separated out within an exponent:  </p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\exp(a+b) = \\exp(a) \\cdot \\exp(b)&quot;,&quot;id&quot;:&quot;WCFZVUDIQE&quot;}" data-component-name="LatexBlockToDOM"></div><p>Another way to look at this is that if you have an exponent multiplied by a term (like a weight), this is equivalent to adding the term with a log inside the exponent: </p><p>\[\exp(a) \cdot b = \exp(a + \log{b})\].  </p><p>Therefore, instead of multiplying the exponentiated scores by the area weight, we can move the area weight into the exponentiation term. As we have just seen, this can be done by converting the multiplication of the area weights into an addition of a logarithmic term:</p><p>\[\tilde{\alpha}_j = \frac{\exp(s_j + \log{\omega_j})}{\sum_i\exp(s_i + \log{\omega_i})}\]</p><p>(we typically add a small number to the weight before the log to avoid \(\omega_j\) being exactly zero, since \(\log(0)\) is undefined).</p><p>And recall that we&#8217;ve already found the weights of the area: they are proportional to the change in the area in the grid, \(\omega_j \propto \sin(\theta_j)\). We only need the weights to be proportional to \(\sin(\theta_j)\) because any uniform constant factor, such as the common factors \(d\theta\) and \(d\phi\), multiplies every cell equally and cancels out when you normalize the weights. In other words, only relative variation with latitude matters; and this is exactly what \(\sin(\theta_j)\) represents.</p><h2>6. Why do we treat tokens in language models as if they come from a Euclidean space?</h2><p>We do not apply any sort of weighting schemes or area representation to token embeddings in language models. Why? </p><p>Vector embeddings for words are Euclidean by construction. We map each token into a vector in some high-dimensional space and perform operations like dot products assuming standard linear geometry. This works because embeddings are trained to approximately <a href="https://informatics.ed.ac.uk/news-events/news/news-archive/king-man-woman-queen-the-hidden-algebraic-struct">follow</a> this Euclidean geometry.</p><p>That is not to say that this is &#8220;optimal&#8221;. It could be that treating vectors in non-Euclidean space would be a much better choice, as discussed in various <a href="https://arxiv.org/html/2504.08896v1">works</a>.</p>]]></content:encoded></item></channel></rss>