<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Multihead Attention on AIBussin — AI applications, systems and books</title><link>https://aibussin.com/tags/multihead-attention/</link><description>Recent content in Multihead Attention on AIBussin — AI applications, systems and books</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sat, 29 Aug 2026 10:00:00 +0100</lastBuildDate><atom:link href="https://aibussin.com/tags/multihead-attention/index.xml" rel="self" type="application/rss+xml"/><item><title>Attention: Which Position Is Comparing With Which?</title><link>https://aibussin.com/books/pytorch-from-first-principles/10-chapter/</link><pubDate>Sat, 29 Aug 2026 10:00:00 +0100</pubDate><guid>https://aibussin.com/books/pytorch-from-first-principles/10-chapter/</guid><description>&lt;p&gt;Here is a tensor of sequence representations and the line that splits it into heads.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;B, T, E, Nh &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;4&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;8&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;2&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Dh &lt;span style="color:#f92672"&gt;=&lt;/span&gt; E &lt;span style="color:#f92672"&gt;//&lt;/span&gt; Nh &lt;span style="color:#75715e"&gt;# 4&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;x &lt;span style="color:#f92672"&gt;=&lt;/span&gt; torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;arange(B &lt;span style="color:#f92672"&gt;*&lt;/span&gt; T &lt;span style="color:#f92672"&gt;*&lt;/span&gt; E)&lt;span style="color:#f92672"&gt;.&lt;/span&gt;reshape(B, T, E)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;heads &lt;span style="color:#f92672"&gt;=&lt;/span&gt; x&lt;span style="color:#f92672"&gt;.&lt;/span&gt;reshape(B, Nh, T, Dh)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;x: (1, 4, 8)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;heads: (1, 2, 4, 4)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That is exactly the shape multi-head attention wants: batch, heads, positions, head dimension. Nothing raised. Every shape assertion passes.&lt;/p&gt;&#10;&lt;p&gt;Now the same split written the other way:&lt;/p&gt;</description></item><item><title>PyTorch Attention Shapes: Q, K, V, Multi-Head Attention Masks and Transformer Dimension Errors</title><link>https://aibussin.com/post/pytorch-zero-to-hero-07/</link><pubDate>Sat, 08 Aug 2026 13:30:00 +0100</pubDate><guid>https://aibussin.com/post/pytorch-zero-to-hero-07/</guid><description>&lt;h2 id="pytorch-zero-to-hero--step-07"&gt;PyTorch: Zero to Hero — Step 07&lt;/h2&gt;&#10;&lt;p&gt;Attention code is where tensor-shape mistakes stop being annoying and start becoming architectural.&lt;/p&gt;&#10;&lt;p&gt;A CNN usually makes its dimensional assumptions fairly obvious. Attention does not.&lt;/p&gt;&#10;&lt;p&gt;A tensor that starts as:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;(batch, sequence, embedding)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;is projected into Q, K and V, split into heads, transposed, multiplied, masked, normalized, multiplied again, transposed again, concatenated and projected back to the embedding dimension.&lt;/p&gt;&#10;&lt;p&gt;A single bad &lt;code&gt;view&lt;/code&gt;, &lt;code&gt;transpose&lt;/code&gt;, mask shape or head calculation can produce anything from an immediate runtime error to a model that trains while attending to the wrong tokens.&lt;/p&gt;</description></item></channel></rss>