<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Torch.compile on AIBussin — AI applications, systems and books</title><link>https://aibussin.com/tags/torch.compile/</link><description>Recent content in Torch.compile on AIBussin — AI applications, systems and books</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sat, 29 Aug 2026 17:00:00 +0100</lastBuildDate><atom:link href="https://aibussin.com/tags/torch.compile/index.xml" rel="self" type="application/rss+xml"/><item><title>Performance: What Is the Machine Waiting For?</title><link>https://aibussin.com/books/pytorch-from-first-principles/12-chapter/</link><pubDate>Sat, 29 Aug 2026 16:00:00 +0100</pubDate><guid>https://aibussin.com/books/pytorch-from-first-principles/12-chapter/</guid><description>&lt;p&gt;Here is a benchmark. A model, a batch, a loop, a timer. Nothing exotic.&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;model&lt;span style="color:#f92672"&gt;.&lt;/span&gt;train()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;t0 &lt;span style="color:#f92672"&gt;=&lt;/span&gt; time&lt;span style="color:#f92672"&gt;.&lt;/span&gt;perf_counter()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; _ &lt;span style="color:#f92672"&gt;in&lt;/span&gt; range(&lt;span style="color:#ae81ff"&gt;20&lt;/span&gt;):&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; optimizer&lt;span style="color:#f92672"&gt;.&lt;/span&gt;zero_grad(set_to_none&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;True&lt;/span&gt;)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; loss &lt;span style="color:#f92672"&gt;=&lt;/span&gt; F&lt;span style="color:#f92672"&gt;.&lt;/span&gt;cross_entropy(model(x), y) &lt;span style="color:#75715e"&gt;# MLP, batch 256&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; loss&lt;span style="color:#f92672"&gt;.&lt;/span&gt;backward()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; optimizer&lt;span style="color:#f92672"&gt;.&lt;/span&gt;step()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ms_per_step &lt;span style="color:#f92672"&gt;=&lt;/span&gt; (time&lt;span style="color:#f92672"&gt;.&lt;/span&gt;perf_counter() &lt;span style="color:#f92672"&gt;-&lt;/span&gt; t0) &lt;span style="color:#f92672"&gt;/&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;20&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;1000&lt;/span&gt;&#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;naive loop of 20: 9.491 ms/step reported + 132.0 ms still queued at the final sync -&amp;gt; really 16.093 ms/step&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;naive loop of 100: 14.692 ms/step reported + 138.8 ms still queued at the final sync -&amp;gt; really 16.080 ms/step&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;naive loop of 400: 15.741 ms/step reported + 132.1 ms still queued at the final sync -&amp;gt; really 16.071 ms/step&#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;synchronized + warmed, median of 400 : 16.430 ms/step &amp;lt;- the real number&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;one step, host-visible return only : 4.866 ms/step&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;first training step ever : 20.890 ms&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Read the first line again. The loop ran twenty iterations and reported &lt;code&gt;9.491&lt;/code&gt; ms per step. Then a single &lt;code&gt;torch.cuda.synchronize()&lt;/code&gt; immediately afterward blocked for &lt;code&gt;132&lt;/code&gt; more milliseconds — roughly eight more steps&amp;rsquo; worth of work that the GPU had not finished when the timer stopped. Loop over a hundred iterations instead and the same code reports &lt;code&gt;14.7&lt;/code&gt;. Over four hundred, &lt;code&gt;15.7&lt;/code&gt;. Time one step in isolation and it looks like &lt;code&gt;4.9&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Compilation: Which Assumption Stopped Holding?</title><link>https://aibussin.com/books/pytorch-from-first-principles/13-chapter/</link><pubDate>Sat, 29 Aug 2026 17:00:00 +0100</pubDate><guid>https://aibussin.com/books/pytorch-from-first-principles/13-chapter/</guid><description>&lt;p&gt;Here is an inference service. It loads a model, compiles it once, and serves requests whose sequence length varies from request to request — the ordinary situation for anything that takes text.&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;model &lt;span style="color:#f92672"&gt;=&lt;/span&gt; load_model()&lt;span style="color:#f92672"&gt;.&lt;/span&gt;eval()&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;compiled &lt;span style="color:#f92672"&gt;=&lt;/span&gt; torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;compile(model, dynamic&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;False&lt;/span&gt;) &lt;span style="color:#75715e"&gt;# &amp;#34;dynamic shapes are slow, force static&amp;#34;&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;&lt;span style="color:#66d9ef"&gt;for&lt;/span&gt; request &lt;span style="color:#f92672"&gt;in&lt;/span&gt; stream:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;with&lt;/span&gt; torch&lt;span style="color:#f92672"&gt;.&lt;/span&gt;no_grad():&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; output &lt;span style="color:#f92672"&gt;=&lt;/span&gt; compiled(request&lt;span style="color:#f92672"&gt;.&lt;/span&gt;tokens) &lt;span style="color:#75715e"&gt;# [1, T], T varies per request&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Thirteen requests. Every compiled output agrees with the eager model to a maximum absolute difference of &lt;code&gt;7e-7&lt;/code&gt;. Nothing raises.&lt;/p&gt;</description></item><item><title>PyTorch Performance Debugging: CUDA OOM, Slow Training, GPU Utilization and torch.compile</title><link>https://aibussin.com/post/pytorch-zero-to-hero-09/</link><pubDate>Sat, 08 Aug 2026 13:56:00 +0100</pubDate><guid>https://aibussin.com/post/pytorch-zero-to-hero-09/</guid><description>&lt;h2 id="pytorch-zero-to-hero--step-09"&gt;PyTorch: Zero to Hero — Step 09&lt;/h2&gt;&#10;&lt;p&gt;At this point in the series, the model runs.&lt;/p&gt;&#10;&lt;p&gt;That does not mean it runs well.&lt;/p&gt;&#10;&lt;p&gt;A training loop can be correct and still waste most of the machine.&lt;/p&gt;&#10;&lt;p&gt;A model can fit in memory and still spend half its time waiting on synchronization.&lt;/p&gt;&#10;&lt;p&gt;A &lt;code&gt;torch.compile&lt;/code&gt; call can make code faster, slower, or simply move the bottleneck somewhere else.&lt;/p&gt;&#10;&lt;p&gt;A CUDA out-of-memory error can be caused by the model, the optimizer, activations, fragmentation, a leaked reference, a larger batch, a longer sequence, or an innocent-looking tensor that was kept alive by Python.&lt;/p&gt;</description></item></channel></rss>