<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Transformers on AIBussin — AI applications, systems and books</title><link>https://aibussin.com/tags/transformers/</link><description>Recent content in Transformers on AIBussin — AI applications, systems and books</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sun, 30 Aug 2026 10:00:00 +0100</lastBuildDate><atom:link href="https://aibussin.com/tags/transformers/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>Assembly: A Language Model You Can Interrogate</title><link>https://aibussin.com/books/pytorch-from-first-principles/15-chapter/</link><pubDate>Sun, 30 Aug 2026 10:00:00 +0100</pubDate><guid>https://aibussin.com/books/pytorch-from-first-principles/15-chapter/</guid><description>&lt;p&gt;Here are two training runs of the same small language model on the same data. The only difference is one line in the batching function.&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;CORRECT y = data[i+1 : i+block+1] the next-token shift&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;MISALIGNED y = data[i : i+block] no shift&#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;step correct val misaligned val&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 0 3.6695 2.6469&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 250 1.1038 0.0007&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 500 0.4730 0.0004&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; 750 0.3940 0.0003&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;1000 0.3690 0.0002&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;1250 0.3581 0.0002&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;1500 0.3558 0.0001&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The misaligned run&amp;rsquo;s loss falls to &lt;code&gt;0.0001&lt;/code&gt; — three thousand times lower than the correct run&amp;rsquo;s &lt;code&gt;0.36&lt;/code&gt;. By every number on the dashboard it is the best training run you have ever seen. Nothing raised. The shapes are identical: &lt;code&gt;x&lt;/code&gt; is &lt;code&gt;[B, T]&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt; is &lt;code&gt;[B, T]&lt;/code&gt;, the loss is a finite scalar with a &lt;code&gt;grad_fn&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>PyTorch Zero to Hero 10: Build a Small GPT-Style Language Model From Scratch</title><link>https://aibussin.com/post/pytorch-zero-to-hero-10/</link><pubDate>Sat, 08 Aug 2026 14:03:00 +0100</pubDate><guid>https://aibussin.com/post/pytorch-zero-to-hero-10/</guid><description>&lt;h1 id="build-a-small-gpt-style-language-model-from-scratch-in-pytorch"&gt;Build a Small GPT-Style Language Model From Scratch in PyTorch&lt;/h1&gt;&#10;&lt;p&gt;This is the final post in the &lt;strong&gt;PyTorch: Zero to Hero&lt;/strong&gt; series.&lt;/p&gt;&#10;&lt;p&gt;We have spent the previous posts learning the machinery underneath PyTorch:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;tensors and shapes;&lt;/li&gt;&#10;&lt;li&gt;autograd;&lt;/li&gt;&#10;&lt;li&gt;manual neural networks;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;nn.Module&lt;/code&gt; and parameter registration;&lt;/li&gt;&#10;&lt;li&gt;DataLoader performance;&lt;/li&gt;&#10;&lt;li&gt;convolutional networks;&lt;/li&gt;&#10;&lt;li&gt;attention and masks;&lt;/li&gt;&#10;&lt;li&gt;training failures;&lt;/li&gt;&#10;&lt;li&gt;CUDA performance and &lt;code&gt;torch.compile&lt;/code&gt;.&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Now we put it together.&lt;/p&gt;&#10;&lt;p&gt;The goal is not to download a pretrained model.&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>