<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>tensor - pytorch</title>
    <subtitle>PyTorch, from the API to the silicon</subtitle>
    <link rel="self" type="application/atom+xml" href="https://tensor.khalilli.ai/tags/pytorch/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://tensor.khalilli.ai"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-08-07T00:00:00+00:00</updated>
    <id>https://tensor.khalilli.ai/tags/pytorch/atom.xml</id>
    <entry xml:lang="en">
        <title>The Map</title>
        <published>2026-08-07T00:00:00+00:00</published>
        <updated>2026-08-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              alikhalilli
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://tensor.khalilli.ai/blog/part-0-the-map/"/>
        <id>https://tensor.khalilli.ai/blog/part-0-the-map/</id>
        
        <content type="html" xml:base="https://tensor.khalilli.ai/blog/part-0-the-map/">&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 892px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;three-lines-light.svg?h=1d20d92e38f6fcf4bd4a&quot;
         alt=&quot;a code panel with the three lines x equals torch dot randn, loss equals model of x dot sum, loss dot backward, and an orange arrow pointing down to the words eight floors below this code&quot; width=&quot;892&quot; height=&quot;333&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;three-lines-dark.svg?h=10f8a5b8c0f3b629cc0f&quot;
         alt=&quot;a code panel with the three lines x equals torch dot randn, loss equals model of x dot sum, loss dot backward, and an orange arrow pointing down to the words eight floors below this code&quot; width=&quot;892&quot; height=&quot;333&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 1.&lt;&#x2F;span&gt; the program this whole series is about.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;You have typed something like this a thousand times. This series
exists so that, by its end, you know everything these lines do. All
of it: the Python they touch, the C++ they land in, the graph they
record, the kernels they choose, the memory they use, and the two
clocks they run on. Each of those words gets a plain meaning on its
floor below.&lt;&#x2F;p&gt;
&lt;p&gt;This is Part 0, the map. First we go down through all the layers
once, fast. Then we draw the territory. Then twelve ideas that make
the rest of the codebase predictable. Then how this series works,
and how to read it. Nothing here gets its full story. Everything
here gets a place, and every full story has a numbered part waiting
for it.&lt;&#x2F;p&gt;
&lt;p&gt;One promise before we start. Every measured number in this series
comes from a small script you can run yourself, linked right where
the number appears. I measured these on an Apple M3 Max laptop with
torch 2.11.0 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-1&quot;&gt;1&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. Your numbers will differ. The pattern they make
will not.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-fall&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-fall&quot; aria-label=&quot;Anchor link for: the-fall&quot;&gt;The fall&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;PyTorch is deep. Between your keyboard and the chip there are eight
levels. I will call them floors, and this meter shows all of them.
It returns through the whole series, so you always know how deep you
are.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing tall&quot; data-anim=&quot;descent-gauge&quot; style=&quot;--dw: 273px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;descent-gauge-light.svg?h=51f2b4210b88cb46035a&quot;
         alt=&quot;a vertical depth meter with eight floor marks labeled your code, python, the boundary, the dispatcher, the kernel, the allocator, the queue, the gpu, with an orange marker at the top floor&quot; width=&quot;273&quot; height=&quot;631&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;descent-gauge-dark.svg?h=850c9c7c7ce3358dee68&quot;
         alt=&quot;a vertical depth meter with eight floor marks labeled your code, python, the boundary, the dispatcher, the kernel, the allocator, the queue, the gpu, with an orange marker at the top floor&quot; width=&quot;273&quot; height=&quot;631&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 2.&lt;&#x2F;span&gt; the depth meter. the orange dot marks where you are.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The fastest way to learn a building is to go down through it once
without stopping. That is this section.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;floor-one-python&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#floor-one-python&quot; aria-label=&quot;Anchor link for: floor-one-python&quot;&gt;Floor one: python&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note&quot; aria-label=&quot;floor 2 of 8: python&quot;&gt;
  &lt;svg viewBox=&quot;0 0 14 76&quot; width=&quot;14&quot; height=&quot;76&quot; role=&quot;img&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;3&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;12&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;#EE4C2C&quot;
          &#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;21&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;30&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;39&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;48&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;57&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;66&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
  &lt;&#x2F;svg&gt;
  &lt;div class=&quot;fn-text&quot;&gt;
    &lt;span class=&quot;fn-where&quot;&gt;floor 2 of 8&lt;&#x2F;span&gt;
    &lt;span class=&quot;fn-name&quot;&gt;python&lt;&#x2F;span&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 5, The Machinery&lt;&#x2F;span&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;&lt;code&gt;torch.randn&lt;&#x2F;code&gt; looks like a Python function. Ask Python what it
actually is:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;randn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt;&amp;lt;class&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;builtin_function_or_method&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Python gives that type only to functions written in compiled code.
Compiled code means: code that was translated to machine
instructions before you ever installed it, so there is no Python
body inside it to read, and no line for your debugger to stop on.&lt;&#x2F;p&gt;
&lt;p&gt;So where do those machine instructions live? In shared libraries.
A shared library is a file of compiled code that a program loads
while it runs. They sit inside the torch package on your disk, and
you can look at them (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p0-the-library&quot;&gt;proof&lt;&#x2F;a&gt;):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;torch._C -&amp;gt; _C.cpython-312-darwin.so  (49 KB, the loader)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libtorch_cpu.dylib        206.5 MB   (tensors and kernels)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libtorch_python.dylib      28.5 MB   (the python side of the border)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p0-the-library&quot;&gt;
  &lt;summary&gt;
    &lt;span class=&quot;proof-tri&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;
    &lt;code class=&quot;proof-name&quot;&gt;p0_the_library.py&lt;&#x2F;code&gt;
    &lt;span class=&quot;proof-hint&quot;&gt;the proof, ready to read or run&lt;&#x2F;span&gt;
  &lt;&#x2F;summary&gt;
  &lt;div class=&quot;proof-body&quot;&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;Proof: where the compiled part of pytorch actually lives.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;torch._C is a thin compiled stub; the weight of the framework is in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;the shared libraries next to it. Prints the files and their sizes.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; glob&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; os&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;stub&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;_C&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__file__&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;torch &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__version__&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;torch._C -&amp;gt; &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;os&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;basename&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;stub&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;      f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;os&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;getsize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;stub&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.0f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; KB stub)&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libdir&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; os&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;join&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;os&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;dirname&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;stub&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;lib&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; lib&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;libtorch_cpu.dylib&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;libtorch_python.dylib&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    p&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; os&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;join&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;libdir&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; lib&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; os&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;exists&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;p&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;        print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;lib&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:24s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;os&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;getsize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;p&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:6.1f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; MB&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;ch00&#x2F;proofs&#x2F;p0_the_library.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;Read the sizes, and then look at them:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;the-library&quot; style=&quot;--dw: 862px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-library-light.svg?h=b4b86b5fc046d48e7ab1&quot;
         alt=&quot;a dashed outline labeled your process after import torch contains two blocks drawn to scale by file size: a huge one for libtorch cpu at 206.5 megabytes and a thin one for libtorch python at 28.5 megabytes; a magnifier blows up a six-pixel orange dot into the 49 kilobyte loader; an orange arrow shows torch dot randn jumping straight into the big block; a small inset named your machine holds one process, tied to the large dashed box by a line labeled enlarged&quot; width=&quot;862&quot; height=&quot;574&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-library-dark.svg?h=0860207e9ba4b6950d95&quot;
         alt=&quot;a dashed outline labeled your process after import torch contains two blocks drawn to scale by file size: a huge one for libtorch cpu at 206.5 megabytes and a thin one for libtorch python at 28.5 megabytes; a magnifier blows up a six-pixel orange dot into the 49 kilobyte loader; an orange arrow shows torch dot randn jumping straight into the big block; a small inset named your machine holds one process, tied to the large dashed box by a line labeled enlarged&quot; width=&quot;862&quot; height=&quot;574&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 3.&lt;&#x2F;span&gt; drawn to scale by file size. the part of pytorch that python can see is the orange dot.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The part of PyTorch you can see from Python is a 49 KB file whose
only job is to load the other two. The real body is 235 MB of
compiled code. &lt;code&gt;import torch&lt;&#x2F;code&gt; brings it into your process, and
after that, calling &lt;code&gt;torch.randn&lt;&#x2F;code&gt; means jumping into that body.
Today we only need to know these files exist.&lt;&#x2F;p&gt;
&lt;p&gt;This is the first honest surprise of the codebase: the Python you
write all day is the smallest layer of it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-boundary&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-boundary&quot; aria-label=&quot;Anchor link for: the-boundary&quot;&gt;The boundary&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note&quot; aria-label=&quot;floor 3 of 8: the boundary&quot;&gt;
  &lt;svg viewBox=&quot;0 0 14 76&quot; width=&quot;14&quot; height=&quot;76&quot; role=&quot;img&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;3&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;12&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;21&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;#EE4C2C&quot;
          &#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;30&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;39&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;48&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;57&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;66&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
  &lt;&#x2F;svg&gt;
  &lt;div class=&quot;fn-text&quot;&gt;
    &lt;span class=&quot;fn-where&quot;&gt;floor 3 of 8&lt;&#x2F;span&gt;
    &lt;span class=&quot;fn-name&quot;&gt;the boundary&lt;&#x2F;span&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 5, The Machinery&lt;&#x2F;span&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;The call leaves Python at once. Where does it land?&lt;&#x2F;p&gt;
&lt;p&gt;In a C++ function named &lt;code&gt;THPVariable_randn&lt;&#x2F;code&gt;, inside that 28.5 MB
library from the last floor. And here is a strange fact you can
keep: this function does not exist in the PyTorch repository. Clone
the repo, search for the name, and you find nothing. A program
writes this function during the build, together with thousands of
its siblings. Idea 4 below explains why, and Part 5 shows the
program that does the writing.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;the-boundary&quot; style=&quot;--dw: 882px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-boundary-light.svg?h=b68b8ba261e887735d95&quot;
         alt=&quot;two territories labeled python and c++ separated by a wall with one gate, an orange call arrow crossing through the gate into a box labeled THPVariable_randn&quot; width=&quot;882&quot; height=&quot;385&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-boundary-dark.svg?h=9dc0492a803603319c1c&quot;
         alt=&quot;two territories labeled python and c++ separated by a wall with one gate, an orange call arrow crossing through the gate into a box labeled THPVariable_randn&quot; width=&quot;882&quot; height=&quot;385&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 4.&lt;&#x2F;span&gt; the border between the two languages. every tensor operation crosses it.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Crossing this border costs time. To see the cost alone, time the
smallest possible operation, where almost no arithmetic hides it
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p2-dispatch-cost&quot;&gt;proof&lt;&#x2F;a&gt;):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;add, 1 element   :    0.538 microseconds per call&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;add, 4M elements :  337.264 microseconds per call&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p2-dispatch-cost&quot;&gt;
  &lt;summary&gt;
    &lt;span class=&quot;proof-tri&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;
    &lt;code class=&quot;proof-name&quot;&gt;p2_dispatch_cost.py&lt;&#x2F;code&gt;
    &lt;span class=&quot;proof-hint&quot;&gt;the proof, ready to read or run&lt;&#x2F;span&gt;
  &lt;&#x2F;summary&gt;
  &lt;div class=&quot;proof-body&quot;&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;Proof: the fixed cost of one eager op, and why size hides it.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Times the same `a + b` at two sizes. The one-element add is nearly&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;pure machinery (dispatch, wrapping, allocation); the 4M-element add is&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;nearly pure arithmetic. CPU, single process.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; per_op_us&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; iters&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;    # warmup&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; range&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; range&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;iters&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; iters&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1e6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tiny&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; per_op_us&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 200_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;big_n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 4_000_000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;big&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; per_op_us&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;big_n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;big_n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;torch &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__version__&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;, cpu&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;add, 1 element   : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;tiny&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8.3f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; us&#x2F;op&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;add, 4M elements : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;big&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8.3f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; us&#x2F;op&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;machinery share of the tiny op: ~all of it&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;ops&#x2F;sec you can issue from python: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1e6&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;tiny&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:,.0f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# The sweep behind the toll meter: the same add at twelve sizes,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# 1 to 4M elements in powers of four. Every dot on the widget&amp;#39;s&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# axis is one line of this output.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; json&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sweep&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-punctuation&quot;&gt; = []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; k&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; range&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;12&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; k&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    iters&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; min&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;200_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 40_000_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    us&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; per_op_us&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span&gt; iters&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    sweep&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;append&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;({&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;us&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; round&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;us&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)})&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;add, &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;9,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; elements : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;us&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:9.3f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; us&#x2F;op&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;JSON_SWEEP=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; json&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;dumps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;sweep&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;ch00&#x2F;proofs&#x2F;p2_dispatch_cost.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;The one-element add does almost no math. So its 0.54 microseconds
is almost pure crossing cost: leave Python, check the arguments,
build the result object, return. Half a microsecond sounds like
nothing. It means Python can issue at most about 1.9 million
operations per second, and a single training step contains
thousands of operations. Keep this number. It returns in Idea 6.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-dispatcher&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-dispatcher&quot; aria-label=&quot;Anchor link for: the-dispatcher&quot;&gt;The dispatcher&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note&quot; aria-label=&quot;floor 4 of 8: the dispatcher&quot;&gt;
  &lt;svg viewBox=&quot;0 0 14 76&quot; width=&quot;14&quot; height=&quot;76&quot; role=&quot;img&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;3&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;12&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;21&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;30&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;#EE4C2C&quot;
          &#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;39&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;48&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;57&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;66&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
  &lt;&#x2F;svg&gt;
  &lt;div class=&quot;fn-text&quot;&gt;
    &lt;span class=&quot;fn-where&quot;&gt;floor 4 of 8&lt;&#x2F;span&gt;
    &lt;span class=&quot;fn-name&quot;&gt;the dispatcher&lt;&#x2F;span&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 5, The Machinery&lt;&#x2F;span&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;Under the border, the call reaches the strangest machine in
PyTorch: the dispatcher. The dispatcher is the router that decides,
for every operation, which pieces of code run and in what order.&lt;&#x2F;p&gt;
&lt;p&gt;Look at what it must decide. Your three lines never said &quot;record
gradients&quot;. No &lt;code&gt;if&lt;&#x2F;code&gt; statement in your code turns that on. Yet
somewhere, something decided that this matrix multiplication should
be remembered for &lt;code&gt;backward()&lt;&#x2F;code&gt;. That something is the dispatcher.
Every operation passes down through a fixed stack of layers. Each
layer can act on the call, change it, or let it pass unchanged.
Autograd, the part of PyTorch that computes gradients, is one such
layer. Mixed precision is another. On this run, only autograd is
awake.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;the-stack&quot; style=&quot;--dw: 752px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-stack-light.svg?h=3b09d2a81b7677452a06&quot;
         alt=&quot;an orange call passes down through stacked layers labeled argument parsing, autograd, autocast, functionalization, cpu backend; the autograd layer is highlighted and a side box shows the node AddmmBackward0 written to the graph&quot; width=&quot;752&quot; height=&quot;570&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-stack-dark.svg?h=7619f248b99bfdf3efaa&quot;
         alt=&quot;an orange call passes down through stacked layers labeled argument parsing, autograd, autocast, functionalization, cpu backend; the autograd layer is highlighted and a side box shows the node AddmmBackward0 written to the graph&quot; width=&quot;752&quot; height=&quot;570&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 5.&lt;&#x2F;span&gt; four layers touch your call before any arithmetic starts. only the highlighted one is awake today.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;h3 id=&quot;the-kernel&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-kernel&quot; aria-label=&quot;Anchor link for: the-kernel&quot;&gt;The kernel&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note&quot; aria-label=&quot;floor 5 of 8: the kernel&quot;&gt;
  &lt;svg viewBox=&quot;0 0 14 76&quot; width=&quot;14&quot; height=&quot;76&quot; role=&quot;img&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;3&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;12&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;21&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;30&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;39&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;#EE4C2C&quot;
          &#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;48&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;57&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;66&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
  &lt;&#x2F;svg&gt;
  &lt;div class=&quot;fn-text&quot;&gt;
    &lt;span class=&quot;fn-where&quot;&gt;floor 5 of 8&lt;&#x2F;span&gt;
    &lt;span class=&quot;fn-name&quot;&gt;the kernel&lt;&#x2F;span&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 8, Kernels &amp;amp; Hardware&lt;&#x2F;span&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;At the bottom of the stack, one concrete function is chosen. Chosen
is the right word. This torch build has 3,677 registered operation
names (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p4-micro-proofs&quot;&gt;proof&lt;&#x2F;a&gt; prints
the count), and a name is not a function body. The operation &lt;code&gt;addmm&lt;&#x2F;code&gt;,
the matrix multiplication behind &lt;code&gt;model(x)&lt;&#x2F;code&gt;, has separate bodies
for CPU and for each kind of GPU, for each data type, for dense and
for sparse tensors. A body like this, written for one device and
one data type, is called a kernel. The dispatcher&#x27;s last job is to
pick one:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;kernel-grid&quot; style=&quot;--dw: 808px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;kernel-grid-light.svg?h=7eb71fabb82c2f70fb2f&quot;
         alt=&quot;the name addmm points at a grid of cells: rows for cpu, cuda and mps, columns for float32, float16, bfloat16 and int8; each cell is a separate function body; the cpu float32 cell is highlighted as the one this run uses; a dashed copy of the grid behind it stands for sparse tensors&quot; width=&quot;808&quot; height=&quot;505&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;kernel-grid-dark.svg?h=4e201c93c9c290be94c7&quot;
         alt=&quot;the name addmm points at a grid of cells: rows for cpu, cuda and mps, columns for float32, float16, bfloat16 and int8; each cell is a separate function body; the cpu float32 cell is highlighted as the one this run uses; a dashed copy of the grid behind it stands for sparse tensors&quot; width=&quot;808&quot; height=&quot;505&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 6.&lt;&#x2F;span&gt; one name, a grid of bodies. the dispatcher picks exactly one cell per call.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p4-micro-proofs&quot;&gt;
  &lt;summary&gt;
    &lt;span class=&quot;proof-tri&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;
    &lt;code class=&quot;proof-name&quot;&gt;p4_micro_proofs.py&lt;&#x2F;code&gt;
    &lt;span class=&quot;proof-hint&quot;&gt;the proof, ready to read or run&lt;&#x2F;span&gt;
  &lt;&#x2F;summary&gt;
  &lt;div class=&quot;proof-body&quot;&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;Micro-proofs quoted in Part 0: storage sharing, view errors,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;mutation rewriting history, the no_grad layer, float32 absorption.&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;torch &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__version__&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# 1. a tensor is a window over storage&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;arange&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;6&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; dtype&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float32&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;view&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;same bytes under both:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;data_ptr&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;data_ptr&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;v.stride():&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;stride&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(),&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; v.t().stride():&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;stride&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;try&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;view&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;except&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; RuntimeError&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; e&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;v.t().view(-1) -&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; str&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;split&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# 2. mutation rewrites the recorded program&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; requires_grad&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt;True&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;before add_:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;add_&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;after  add_:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# 3. no_grad removes one dispatcher layer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;with&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;no_grad&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    z&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;inside no_grad, grad_fn:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# 4. float32 absorbs small numbers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tensor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1e8&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;(1e8 + 1) - 1e8 in float32 =&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;, ((&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;item&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# 5. the size of the operation list (idea 3)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;registered operation names:&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;      len&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;_C&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;_dispatch_get_all_op_names&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;ch00&#x2F;proofs&#x2F;p4_micro_proofs.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;The full list of operations lives in one file in the repository:
&lt;a rel=&quot;noopener nofollow noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pytorch&#x2F;pytorch&#x2F;blob&#x2F;v2.11.0&#x2F;aten&#x2F;src&#x2F;ATen&#x2F;native&#x2F;native_functions.yaml&quot;&gt;&lt;code&gt;native_functions.yaml&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-2&quot;&gt;2&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.
Its sibling
&lt;a rel=&quot;noopener nofollow noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pytorch&#x2F;pytorch&#x2F;blob&#x2F;v2.11.0&#x2F;tools&#x2F;autograd&#x2F;derivatives.yaml&quot;&gt;&lt;code&gt;derivatives.yaml&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;
lists the derivative of each operation. Everything else grows from
these two files. No other file in the repository tells you as much
per line.
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;kernel-pick&quot; style=&quot;--dw: 874px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;kernel-pick-light.svg?h=f2e00476ef886066a8c5&quot;
         alt=&quot;a long list of operation names narrowing through a funnel into a chooser labeled device cpu dtype float32, which points to a single highlighted box labeled one kernel actually multiplying&quot; width=&quot;874&quot; height=&quot;473&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;kernel-pick-dark.svg?h=0d75ea9e06815a9ac83a&quot;
         alt=&quot;a long list of operation names narrowing through a funnel into a chooser labeled device cpu dtype float32, which points to a single highlighted box labeled one kernel actually multiplying&quot; width=&quot;874&quot; height=&quot;473&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 7.&lt;&#x2F;span&gt; 3,677 names on the left. one function body on the right. the funnel is the dispatcher&#x27;s last job.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;&#x2F;p&gt;
&lt;p&gt;One floor down sits memory. &lt;code&gt;torch.randn(64, 128)&lt;&#x2F;code&gt; needs 32,768
bytes: 64 rows times 128 numbers times 4 bytes per number. On the
CPU this is an ordinary allocation. On a GPU it is not. There,
PyTorch runs its own allocator, a keeper of memory that asks the
GPU driver for large blocks once and then reuses them, because
asking the driver every time is slow. This allocator decides when
you run out of memory and what the error means. Part 4 examines it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-two-clocks&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-two-clocks&quot; aria-label=&quot;Anchor link for: the-two-clocks&quot;&gt;The two clocks&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note&quot; aria-label=&quot;floor 7 of 8: the queue&quot;&gt;
  &lt;svg viewBox=&quot;0 0 14 76&quot; width=&quot;14&quot; height=&quot;76&quot; role=&quot;img&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;3&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;12&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;21&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;30&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;39&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;48&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;57&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;#EE4C2C&quot;
          &#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;66&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
  &lt;&#x2F;svg&gt;
  &lt;div class=&quot;fn-text&quot;&gt;
    &lt;span class=&quot;fn-where&quot;&gt;floor 7 of 8&lt;&#x2F;span&gt;
    &lt;span class=&quot;fn-name&quot;&gt;the queue&lt;&#x2F;span&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 4, Seeing PyTorch&lt;&#x2F;span&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;Here the story splits in two. What follows is the single most
useful performance fact in PyTorch.&lt;&#x2F;p&gt;
&lt;p&gt;On a GPU, your Python line does not do the work. It requests the
work, and the request returns at once. The GPU does the work on its
own clock, while Python continues. I measured it on this machine&#x27;s
GPU (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p3-two-timelines&quot;&gt;proof&lt;&#x2F;a&gt;):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;time to request 50 matrix multiplications :   1.58 ms&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;time until the work was actually done     :  73.83 ms&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;python was free during                    :  72.25 ms  (98%)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p3-two-timelines&quot;&gt;
  &lt;summary&gt;
    &lt;span class=&quot;proof-tri&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;
    &lt;code class=&quot;proof-name&quot;&gt;p3_two_timelines.py&lt;&#x2F;code&gt;
    &lt;span class=&quot;proof-hint&quot;&gt;the proof, ready to read or run&lt;&#x2F;span&gt;
  &lt;&#x2F;summary&gt;
  &lt;div class=&quot;proof-body&quot;&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;Proof: the CPU runs ahead of the GPU.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Queues 50 large matmuls on the MPS device and measures two times:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;how long Python took to *ask* for the work, and how long the work&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;actually took. The difference is the gap the chapter draws.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;assert&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;backends&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;is_available&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(),&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;needs an Apple-silicon GPU&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dev&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;device&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;mps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;randn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2048&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2048&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; device&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;dev&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;randn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2048&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2048&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; device&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;dev&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; range&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;5&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;          # warmup&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;    (&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; @&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;synchronize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;t0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; range&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;50&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    c&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; @&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;t_queue&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;synchronize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;t_done&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;torch &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__version__&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;, mps&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;time to queue 50 matmuls : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;t_queue&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1e3&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8.2f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ms&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;time until work finished : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;t_done&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1e3&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8.2f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ms&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;python was free for      : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;t_done&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;t_queue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;*&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1e3&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8.2f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ms (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;t_done&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;t_queue&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;t_done&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.0%&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; of the wall time)&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# The three ways to read the loss, measured, for the two-clocks&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;# widget: never, once at the end, after every step.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; json&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; run_mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt;mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable z-parameter z-function&quot;&gt; iters&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;50&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; range&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;5&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;        (&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; @&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;synchronize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t_free&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; range&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;iters&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        c&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; @&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;every&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            c&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;item&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t_q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span&gt; mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;once&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        c&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;item&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;synchronize&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    total&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;perf_counter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; {&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;queue_ms&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; round&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;t_q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1e3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;total_ms&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; round&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;total&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1e3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;free_ms&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; round&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;((&lt;&#x2F;span&gt;&lt;span&gt;total&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t_q&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1e3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;modes&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-punctuation&quot;&gt; = [&lt;&#x2F;span&gt;&lt;span&gt;run_mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;m&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; m&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;never&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;once&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;every&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; m&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; modes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;read &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;m&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;mode&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;5&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;: total &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;m&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;total_ms&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8.2f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ms, &amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;          f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;python busy &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;m&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;queue_ms&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8.2f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ms&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;JSON_MODES=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; json&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;dumps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;modes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;ch00&#x2F;proofs&#x2F;p3_two_timelines.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;two-timelines&quot; style=&quot;--dw: 922px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;two-timelines-light.svg?h=b6831c5069fdb6b94e63&quot;
         alt=&quot;two horizontal timelines: the cpu lane shows a small orange block for requesting then a long free stretch; the gpu lane below shows contiguous orange work blocks spanning 74 milliseconds&quot; width=&quot;922&quot; height=&quot;379&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;two-timelines-dark.svg?h=f8f73ed3a0d96c8145ce&quot;
         alt=&quot;two horizontal timelines: the cpu lane shows a small orange block for requesting then a long free stretch; the gpu lane below shows contiguous orange work blocks spanning 74 milliseconds&quot; width=&quot;922&quot; height=&quot;379&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 8.&lt;&#x2F;span&gt; two clocks, one program. the cpu requested everything in the first two milliseconds; the gpu needed seventy-two more to finish.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Python asked for all fifty multiplications in under two
milliseconds, then waited, free, while the GPU computed for another
seventy-two. On the CPU there is no such split; the math happens
before your line returns. On any accelerator, the split is the
normal state of the program.&lt;&#x2F;p&gt;
&lt;p&gt;This is why eager PyTorch is fast enough to use: Python runs ahead
and the GPU never waits for it. It is also why simple timing code
gives wrong answers, and why one &lt;code&gt;loss.item()&lt;&#x2F;code&gt; inside a training
loop can slow the whole step. &lt;code&gt;.item()&lt;&#x2F;code&gt; needs the actual number. The
number sits at the end of a queue of work the GPU has not finished
yet, so Python must stop and wait for the whole queue:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;the-queue&quot; style=&quot;--dw: 737px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-queue-light.svg?h=5b4b21f850d760be5297&quot;
         alt=&quot;a code panel for python at the top right, its print of loss dot item held in a dashed waiting box; below it an open channel of six tickets named matmul, add, relu, matmul, add and sum, the newest joining under python, an orange dashed line tying the waiting print to the sum ticket; at the bottom left the gpu takes the oldest ticket first&quot; width=&quot;737&quot; height=&quot;376&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-queue-dark.svg?h=cd91478f4bc135dada6c&quot;
         alt=&quot;a code panel for python at the top right, its print of loss dot item held in a dashed waiting box; below it an open channel of six tickets named matmul, add, relu, matmul, add and sum, the newest joining under python, an orange dashed line tying the waiting print to the sum ticket; at the bottom left the gpu takes the oldest ticket first&quot; width=&quot;737&quot; height=&quot;376&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 9.&lt;&#x2F;span&gt; the queue between the two clocks. the number python asked for is the last ticket, so every ticket ahead of it must finish first.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;div class=&quot;widget&quot; data-widget=&quot;two-clocks&quot;&gt;
  &lt;div class=&quot;w-frame&quot;&gt;
    &lt;span class=&quot;w-tag&quot;&gt;Interactive&lt;&#x2F;span&gt;
    &lt;div class=&quot;w-mount&quot;&gt;
      &lt;img class=&quot;drawing-light&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;clocks-meet-light.svg?h=4684bc29d46ae6858fab&quot;
           alt=&quot;the two timelines again: the cpu requests work, then stands in a dashed waiting box labeled loss dot item, while the gpu is still computing; a dashed vertical line marks the meeting point where both clocks must agree before python continues&quot; width=&quot;922&quot; height=&quot;285&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;img class=&quot;drawing-dark&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;clocks-meet-dark.svg?h=306392118a3e85672b37&quot;
           alt=&quot;the two timelines again: the cpu requests work, then stands in a dashed waiting box labeled loss dot item, while the gpu is still computing; a dashed vertical line marks the meeting point where both clocks must agree before python continues&quot; width=&quot;922&quot; height=&quot;285&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;p class=&quot;w-note&quot;&gt;this instrument needs javascript; the still drawing stands in.&lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
  &lt;&#x2F;div&gt;
  &lt;p class=&quot;w-cap&quot;&gt;&lt;span class=&quot;fig-label&quot;&gt;Interactive 1.&lt;&#x2F;span&gt; the two clocks, measured. choose how often the loop reads the loss; the lanes show who waits, and for how long.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Part 4 teaches honest measurement on top of exactly this picture.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-turn&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-turn&quot; aria-label=&quot;Anchor link for: the-turn&quot;&gt;The turn&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note&quot; aria-label=&quot;floor 4 of 8: the autograd layer&quot;&gt;
  &lt;svg viewBox=&quot;0 0 14 76&quot; width=&quot;14&quot; height=&quot;76&quot; role=&quot;img&quot; aria-hidden=&quot;true&quot;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;3&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;12&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;21&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;30&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;#EE4C2C&quot;
          &#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;39&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;48&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;57&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
    &lt;rect x=&quot;4&quot; y=&quot;66&quot; width=&quot;7&quot; height=&quot;6&quot;
          fill=&quot;currentColor&quot;
          opacity=&quot;0.25&quot;&#x2F;&gt;
  &lt;&#x2F;svg&gt;
  &lt;div class=&quot;fn-text&quot;&gt;
    &lt;span class=&quot;fn-where&quot;&gt;floor 4 of 8&lt;&#x2F;span&gt;
    &lt;span class=&quot;fn-name&quot;&gt;the autograd layer&lt;&#x2F;span&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 2, Autograd&lt;&#x2F;span&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;Line three: &lt;code&gt;loss.backward()&lt;&#x2F;code&gt;. Nothing so far explains how this
line can work. The forward computation is over. How does PyTorch
know what to differentiate?&lt;&#x2F;p&gt;
&lt;p&gt;It knows because the forward pass had a second job. Every time an
operation passed the autograd layer of the dispatcher, a small
record was written: which operation ran, and which recorded steps
produced its inputs. Records that point at
records form a graph, and that word here always means exactly this
recorded structure. By the time &lt;code&gt;loss&lt;&#x2F;code&gt; exists, its graph exists
too (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p1-graph-chain&quot;&gt;proof&lt;&#x2F;a&gt;):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;loss.grad_fn     = SumBackward0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SumBackward0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  AddmmBackward0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    AccumulateGrad&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p1-graph-chain&quot;&gt;
  &lt;summary&gt;
    &lt;span class=&quot;proof-tri&quot; aria-hidden=&quot;true&quot;&gt;&lt;&#x2F;span&gt;
    &lt;code class=&quot;proof-name&quot;&gt;p1_graph_chain.py&lt;&#x2F;code&gt;
    &lt;span class=&quot;proof-hint&quot;&gt;the proof, ready to read or run&lt;&#x2F;span&gt;
  &lt;&#x2F;summary&gt;
  &lt;div class=&quot;proof-body&quot;&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;Proof: loss.backward() walks a graph that forward quietly recorded.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Builds the chapter&amp;#39;s three-line program and prints the autograd graph&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;that exists before backward is ever called.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;nn&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; nn&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;manual_seed&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;model&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; nn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Sequential&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;nn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Linear&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;128&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 256&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;),&lt;&#x2F;span&gt;&lt;span&gt; nn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ReLU&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(),&lt;&#x2F;span&gt;&lt;span&gt; nn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Linear&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;256&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;randn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;64&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 128&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;loss&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; model&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;sum&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;torch &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__version__&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;x.grad_fn        = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;f&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;loss.grad_fn     = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;loss&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;node&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; depth&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; loss&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt; node&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; is not&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; None&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; and&lt;&#x2F;span&gt;&lt;span&gt; depth&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 10&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;  &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; depth&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;node&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    nexts&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-punctuation&quot;&gt; = [&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; node&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;next_functions&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; is not&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; None&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    node&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; nexts&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; nexts&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-constant z-language&quot;&gt; else None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    depth&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;ch00&#x2F;proofs&#x2F;p1_graph_chain.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;the-climb&quot; style=&quot;--dw: 822px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-climb-light.svg?h=7ca03455160c26604395&quot;
         alt=&quot;on the left a staircase descends through x times w, plus b, and dot sum, and each fall writes its own record into a tall box named the graph, every record pointing an arrow at the record that produced its inputs, with loss dot grad underscore fn entering from below as the handle; on the right an orange staircase climbs the same three records in reverse, and gradients arrive at the inputs through AccumulateGrad&quot; width=&quot;822&quot; height=&quot;497&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-climb-dark.svg?h=467f2a4453b03d357512&quot;
         alt=&quot;on the left a staircase descends through x times w, plus b, and dot sum, and each fall writes its own record into a tall box named the graph, every record pointing an arrow at the record that produced its inputs, with loss dot grad underscore fn entering from below as the handle; on the right an orange staircase climbs the same three records in reverse, and gradients arrive at the inputs through AccumulateGrad&quot; width=&quot;822&quot; height=&quot;497&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 10.&lt;&#x2F;span&gt; the forward pass goes down and writes the graph. backward climbs exactly what was written, and nothing else.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;&lt;code&gt;backward()&lt;&#x2F;code&gt; invents nothing. It walks the graph from the loss back
to your inputs, runs each recorded derivative, and stores the
results in &lt;code&gt;.grad&lt;&#x2F;code&gt;. The walk ends at &lt;code&gt;AccumulateGrad&lt;&#x2F;code&gt;, the record
that does the storing. And it starts nowhere else: &lt;code&gt;x.grad_fn&lt;&#x2F;code&gt; is
&lt;code&gt;None&lt;&#x2F;code&gt;, because &lt;code&gt;x&lt;&#x2F;code&gt; was created directly, not computed.&lt;&#x2F;p&gt;
&lt;p&gt;One question should bother you here. A derivative needs values. The
derivative of a matrix multiplication needs the matrices that were
multiplied, and the forward pass is long over. Write the derivative
out and the need is visible:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;derivative-needs&quot; style=&quot;--dw: 712px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;derivative-needs-light.svg?h=03857291930085d5c5d3&quot;
         alt=&quot;the forward line y equals x at w with x as a blue tile and w as a warm tile; below a dashed line labeled forward is over, the two backward formulas: grad underscore w equals x transposed at g, and grad underscore x equals g at w transposed, with the same blue and warm tiles appearing inside them; dashed threads carry each tile across the line into its formula&quot; width=&quot;712&quot; height=&quot;377&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;derivative-needs-dark.svg?h=e2cfd7933e11050152d3&quot;
         alt=&quot;the forward line y equals x at w with x as a blue tile and w as a warm tile; below a dashed line labeled forward is over, the two backward formulas: grad underscore w equals x transposed at g, and grad underscore x equals g at w transposed, with the same blue and warm tiles appearing inside them; dashed threads carry each tile across the line into its formula&quot; width=&quot;712&quot; height=&quot;377&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 11.&lt;&#x2F;span&gt; the derivative of x @ w, written out. the formulas contain x and w themselves; whatever forward used, backward needs again.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;So where are they? They were saved, next to the records, during
the forward pass:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;saved-for-backward&quot; style=&quot;--dw: 736px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;saved-for-backward-light.svg?h=f01c4dfa917c492f0ca2&quot;
         alt=&quot;the three records chained downward, each with a shelf beside it: SumBackward0 has a slim dashed shelf saying nothing; AddmmBackward0 has a large orange shelf holding the blue x tile and the warm w tile from the previous figure, with times every layer of your model written beneath it; AccumulateGrad has a slim dashed nothing shelf; below, the law that the saved values are used once at backward and freed&quot; width=&quot;736&quot; height=&quot;422&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;saved-for-backward-dark.svg?h=1c97f1c6181df2bae70a&quot;
         alt=&quot;the three records chained downward, each with a shelf beside it: SumBackward0 has a slim dashed shelf saying nothing; AddmmBackward0 has a large orange shelf holding the blue x tile and the warm w tile from the previous figure, with times every layer of your model written beneath it; AccumulateGrad has a slim dashed nothing shelf; below, the law that the saved values are used once at backward and freed&quot; width=&quot;736&quot; height=&quot;422&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 12.&lt;&#x2F;span&gt; what each record kept. this is where the memory of a training run actually goes.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;So the forward pass silently decides how much memory training costs.
Part 2 shows the exact saving rules. Part 4 shows how to watch it
happen. And a method called activation checkpointing trades that
memory for extra compute; it has its own chapter in Part 2.&lt;&#x2F;p&gt;
&lt;p&gt;Carry one sentence out of this section: backward can only walk what
forward wrote. It sounds small. In Part 9 it becomes the rule that
decides which GPUs in a cluster must talk to each other.&lt;&#x2F;p&gt;
&lt;p&gt;That was the whole fall: a name, a border, a stack of layers, a
chosen kernel, a keeper of memory, two clocks, and a graph that is
walked backward. Now the territory, properly.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-territory&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-territory&quot; aria-label=&quot;Anchor link for: the-territory&quot;&gt;The territory&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;PyTorch is built in layers, and each layer speaks only to its
neighbors. Every box below is at least one part of this series.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;universe-map&quot; style=&quot;--dw: 880px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;universe-map-light.svg?h=216bd97c3ee304e7a12d&quot;
         alt=&quot;a stacked map of pytorch: the ecosystem on top with the logos of transformers, lightning, vllm, deepspeed and trl, then deployment, distributed and compiler towers, then the python api, the highlighted dispatcher, the aten kernels, the c10 core, and hardware at the bottom, with an orange line running down the left edge marking the fall from the previous section&quot; width=&quot;880&quot; height=&quot;609&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;universe-map-dark.svg?h=7987acb4892ec5ab6404&quot;
         alt=&quot;a stacked map of pytorch: the ecosystem on top with the logos of transformers, lightning, vllm, deepspeed and trl, then deployment, distributed and compiler towers, then the python api, the highlighted dispatcher, the aten kernels, the c10 core, and hardware at the bottom, with an orange line running down the left edge marking the fall from the previous section&quot; width=&quot;880&quot; height=&quot;609&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 13.&lt;&#x2F;span&gt; the whole system on one sheet. the orange line on the left is the path we just took.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The same territory, seen as folders in the repository. If you ever
open the codebase, this is the map that stops you from being lost:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;repo-city&quot; style=&quot;--dw: 843px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;repo-city-light.svg?h=a34883f942fc98d7290a&quot;
         alt=&quot;the pytorch repository as two river banks: torch and the compiler folders on the python side; aten and the imported third party kernels on the c++ side; torch csrc as the one bridge over the water; below both banks one wide layer named c10 carries support columns from every building, and torchgen sits underneath, writing generated code at build time&quot; width=&quot;843&quot; height=&quot;671&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;repo-city-dark.svg?h=b61c722f81698e81bcaf&quot;
         alt=&quot;the pytorch repository as two river banks: torch and the compiler folders on the python side; aten and the imported third party kernels on the c++ side; torch csrc as the one bridge over the water; below both banks one wide layer named c10 carries support columns from every building, and torchgen sits underneath, writing generated code at build time&quot; width=&quot;843&quot; height=&quot;671&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 14.&lt;&#x2F;span&gt; the repository as two river banks. the river is the boundary from figure 4; torch&#x2F;csrc&#x2F; is its one bridge; and both banks stand on the same ground, c10&#x2F;, where Tensor and Storage themselves live.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Three facts about this map save you weeks. First: &lt;code&gt;torch&#x2F;&lt;&#x2F;code&gt; is
plain Python, and you can read every file in it today. Second:
&lt;code&gt;aten&#x2F;&lt;&#x2F;code&gt; and &lt;code&gt;c10&#x2F;&lt;&#x2F;code&gt; are C++; the tensors, the kernels and the
dispatcher live there, and &lt;code&gt;torch&#x2F;csrc&#x2F;&lt;&#x2F;code&gt; is the single bridge that
connects the two languages. Third: &lt;code&gt;torchgen&#x2F;&lt;&#x2F;code&gt; is the program from
the boundary floor, the one that writes code during the build. The
repository you read is the input. The library you run is the
output. That is why searching the repository for
&lt;code&gt;THPVariable_randn&lt;&#x2F;code&gt; finds nothing:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;iceberg&quot; style=&quot;--dw: 932px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;iceberg-light.svg?h=df755ceeccebd25d9822&quot;
         alt=&quot;an iceberg: a small tip above the waterline labeled the repo you clone, and a much larger mass below labeled the code that runs, written by torchgen at build time&quot; width=&quot;932&quot; height=&quot;607&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;iceberg-dark.svg?h=f88eb29a898f918ceffb&quot;
         alt=&quot;an iceberg: a small tip above the waterline labeled the repo you clone, and a much larger mass below labeled the code that runs, written by torchgen at build time&quot; width=&quot;932&quot; height=&quot;607&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 15.&lt;&#x2F;span&gt; the repository is the part above the waterline. the code your process runs is the larger part below it.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Above the core sits the ecosystem. It looks endless, but it has a
simple shape: every library attaches to PyTorch at a specific,
nameable place. Know the attachment places and you know the
ecosystem.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;constellation&quot; style=&quot;--dw: 971px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;constellation-light.svg?h=65fde1d00db10f564bd7&quot;
         alt=&quot;pytorch drawn as a core with four named ports: nn.Module, optim, distributed, and the op set; libraries on an inner ring attach directly to those ports, with vllm and sglang sharing one dot as the runtime replacers; trl and peft sit on an outer ring attached to transformers instead, showing they build on it rather than on pytorch&quot; width=&quot;971&quot; height=&quot;597&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;constellation-dark.svg?h=26221a376512d863d269&quot;
         alt=&quot;pytorch drawn as a core with four named ports: nn.Module, optim, distributed, and the op set; libraries on an inner ring attach directly to those ports, with vllm and sglang sharing one dot as the runtime replacers; trl and peft sit on an outer ring attached to transformers instead, showing they build on it rather than on pytorch&quot; width=&quot;971&quot; height=&quot;597&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 16.&lt;&#x2F;span&gt; every line points at the exact place a library attaches. trl and peft sit on the outer ring: they build on transformers, not on pytorch.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Read the picture from the center out. &lt;code&gt;transformers&lt;&#x2F;code&gt; builds its
models as &lt;code&gt;nn.Module&lt;&#x2F;code&gt; classes, so if you understand Part 3, you can
read its source. &lt;code&gt;deepspeed&lt;&#x2F;code&gt; replaces the distributed engine, so
its home is Part 9. &lt;code&gt;vllm&lt;&#x2F;code&gt; and &lt;code&gt;sglang&lt;&#x2F;code&gt; keep the model weights and
replace the runtime around them. And &lt;code&gt;trl&lt;&#x2F;code&gt; and &lt;code&gt;peft&lt;&#x2F;code&gt; do not touch
PyTorch directly at all; they build on &lt;code&gt;transformers&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The whole ecosystem fits in one table. The second column names the
place in the PyTorch repository where each family attaches:&lt;&#x2F;p&gt;
&lt;div class=&quot;census-wrap&quot;&gt;&lt;table class=&quot;census&quot;&gt;
  &lt;thead&gt;&lt;tr&gt;&lt;th&gt;attaches at&lt;&#x2F;th&gt;&lt;th&gt;the pytorch side&lt;&#x2F;th&gt;&lt;th&gt;who&lt;&#x2F;th&gt;&lt;th&gt;what they keep, what they bring&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;&lt;td&gt;nn.Module&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;torch&#x2F;nn&#x2F;&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;transformers, diffusers, timm&lt;&#x2F;td&gt;&lt;td&gt;models are Modules; torch runs them&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
    &lt;tr&gt;&lt;td&gt;the training loop&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;torch&#x2F;autograd&#x2F;&lt;&#x2F;code&gt; &lt;code&gt;torch&#x2F;optim&#x2F;&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;lightning, accelerate&lt;&#x2F;td&gt;&lt;td&gt;torch stays the engine; they drive it&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
    &lt;tr&gt;&lt;td&gt;the distributed engine&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;torch&#x2F;distributed&#x2F;&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;deepspeed&lt;&#x2F;td&gt;&lt;td&gt;swaps the engine, brings ZeRO&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
    &lt;tr&gt;&lt;td&gt;the eager runtime&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;torch&#x2F;nn&#x2F;&lt;&#x2F;code&gt; &lt;code&gt;torch&#x2F;library.py&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;vllm, sglang, TensorRT-LLM&lt;&#x2F;td&gt;&lt;td&gt;keep the weights, replace the runtime, each with a csrc&#x2F; of its own kernels&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
    &lt;tr&gt;&lt;td&gt;the operation list&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;aten&#x2F;&lt;&#x2F;code&gt; &lt;code&gt;torch&#x2F;library.py&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;flash-attention, torchvision ops&lt;&#x2F;td&gt;&lt;td&gt;new names on the list&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
    &lt;tr&gt;&lt;td&gt;two floors at once&lt;&#x2F;td&gt;&lt;td&gt;&lt;code&gt;torch&#x2F;autograd&#x2F;&lt;&#x2F;code&gt; + transformers&lt;&#x2F;td&gt;&lt;td&gt;unsloth&lt;&#x2F;td&gt;&lt;td&gt;trains through transformers, brings its own Triton kernels&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
    &lt;tr&gt;&lt;td&gt;only the weights&lt;&#x2F;td&gt;&lt;td&gt;none; the weights file&lt;&#x2F;td&gt;&lt;td&gt;TEI, llama.cpp, MLX&lt;&#x2F;td&gt;&lt;td&gt;left pytorch, kept the weights; llama.cpp re-encodes them to GGUF&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
  &lt;&#x2F;tbody&gt;
&lt;&#x2F;table&gt;&lt;&#x2F;div&gt;
&lt;p&gt;Two rows of the table deserve pictures. The first is the eager
runtime, the thing the serving engines replace:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;the-two-runtimes&quot; style=&quot;--dw: 672px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-two-runtimes-light.svg?h=ce055bab20d87bb71174&quot;
         alt=&quot;left, eager pytorch: three code lines send three orange arrows down through a dashed band named the boundary, the dispatcher, a toll each, landing on three kernels named matmul, relu and sum, with the note three trips, three tolls; right, a serving engine with scheduler, batching and paged kv inside plans once and launches one fused block of matmul plus relu plus sum; both sides stand on one wide slab named the shared ground: torch tensors, nn.Module, the kernels themselves&quot; width=&quot;672&quot; height=&quot;434&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;the-two-runtimes-dark.svg?h=9a11f76f553d775d7495&quot;
         alt=&quot;left, eager pytorch: three code lines send three orange arrows down through a dashed band named the boundary, the dispatcher, a toll each, landing on three kernels named matmul, relu and sum, with the note three trips, three tolls; right, a serving engine with scheduler, batching and paged kv inside plans once and launches one fused block of matmul plus relu plus sum; both sides stand on one wide slab named the shared ground: torch tensors, nn.Module, the kernels themselves&quot; width=&quot;672&quot; height=&quot;434&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 17.&lt;&#x2F;span&gt; two ways to run one model. the engines replace the loop in the middle; the ground is shared.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The second is Triton, the kernel language that appears through the
whole table: PyTorch&#x27;s compiler writes it, and libraries bring
their own:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; data-anim=&quot;triton-and-pytorch&quot; style=&quot;--dw: 738px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;triton-and-pytorch-light.svg?h=e82cfd781c76a1ff2816&quot;
         alt=&quot;a central box named at triton dot jit, gpu code in python syntax, wrapped in a dashed ring named torch dot autograd dot Function, a hand-written backward; torch underscore inductor, the compiler, points in from the left, pytorch writes triton itself; a blue tensor tile points in from the top right, runs on torch tensors through data underscore ptr; an arrow leaves to a small list of matmul, relu and a warm ticket named yours, registered through torch slash library dot py, a new name on the list&quot; width=&quot;738&quot; height=&quot;308&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;triton-and-pytorch-dark.svg?h=75358151b364e2257563&quot;
         alt=&quot;a central box named at triton dot jit, gpu code in python syntax, wrapped in a dashed ring named torch dot autograd dot Function, a hand-written backward; torch underscore inductor, the compiler, points in from the left, pytorch writes triton itself; a blue tensor tile points in from the top right, runs on torch tensors through data underscore ptr; an arrow leaves to a small list of matmul, relu and a warm ticket named yours, registered through torch slash library dot py, a new name on the list&quot; width=&quot;738&quot; height=&quot;308&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 18.&lt;&#x2F;span&gt; triton and pytorch. the compiler writes triton itself; hand-written kernels run on torch tensors and join the list.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Read the table downward and less of PyTorch survives each row. The
last row keeps nothing but the weights file. That is the quiet law
of the ecosystem: the weights outlive the runtime. Part 10 walks
these attachment points one by one.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-twelve-ideas&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-twelve-ideas&quot; aria-label=&quot;Anchor link for: the-twelve-ideas&quot;&gt;The twelve ideas&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Most of PyTorch is not thousands of separate decisions. It is a
small set of ideas, applied everywhere. These twelve make the rest
of the codebase predictable before you read it. Each one returns
later as a full chapter or part. Each one comes with runnable
evidence now; the small proofs share one script
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p4-micro-proofs&quot;&gt;proof&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;1-a-tensor-is-a-window-over-storage&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#1-a-tensor-is-a-window-over-storage&quot; aria-label=&quot;Anchor link for: 1-a-tensor-is-a-window-over-storage&quot;&gt;1. A tensor is a window over storage&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 1, Tensor&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;A tensor does not hold numbers. It holds a description of where to
look: a pointer into one flat block of memory, the sizes of each
dimension, and the strides. A stride is the number of steps to move
in that flat block to reach the next element of a dimension. Two
tensors can look completely different and read the same bytes:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;arange&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;6&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.)&lt;&#x2F;span&gt;&lt;span&gt;; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;view&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;data_ptr&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;data_ptr&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;   # same address in memory&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-language&quot;&gt;True&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;stride&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(),&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;stride&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;     # transpose swapped the strides&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation&quot;&gt;((&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;), (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The transpose moved no data. It swapped two numbers in the
description. Some descriptions are impossible to write down, and
that is exactly why &lt;code&gt;v.t().view(-1)&lt;&#x2F;code&gt; raises an error while
&lt;code&gt;reshape&lt;&#x2F;code&gt; silently copies the data instead. Part 1 opens with this
puzzle and solves it completely.&lt;&#x2F;p&gt;
&lt;div class=&quot;widget&quot; data-widget=&quot;window-machine&quot;&gt;
  &lt;div class=&quot;w-frame&quot;&gt;
    &lt;span class=&quot;w-tag&quot;&gt;Interactive&lt;&#x2F;span&gt;
    &lt;div class=&quot;w-mount&quot;&gt;
      &lt;img class=&quot;drawing-light&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-window-light.svg?h=c13c7d3d3944217d4c80&quot;
           alt=&quot;the tensor a as a 2 by 3 grid, the storage bar of six numbered slots, and a.t() as a 3 by 2 grid; every cell carries a small corner number naming the slot it reads; slot 4 is highlighted and followed by dashed connectors into both tensors&quot; width=&quot;632&quot; height=&quot;553&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;img class=&quot;drawing-dark&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-window-dark.svg?h=53cacf84fd16df56daf3&quot;
           alt=&quot;the tensor a as a 2 by 3 grid, the storage bar of six numbered slots, and a.t() as a 3 by 2 grid; every cell carries a small corner number naming the slot it reads; slot 4 is highlighted and followed by dashed connectors into both tensors&quot; width=&quot;632&quot; height=&quot;553&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;p class=&quot;w-note&quot;&gt;this instrument needs javascript; the still drawing stands in.&lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
  &lt;&#x2F;div&gt;
  &lt;p class=&quot;w-cap&quot;&gt;&lt;span class=&quot;fig-label&quot;&gt;Interactive 2.&lt;&#x2F;span&gt; six numbers, one storage. shape and strides decide which slot every cell reads; view(-1) exists only when the walk matches storage order.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h3 id=&quot;2-autograd-records-a-program-you-never-wrote&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#2-autograd-records-a-program-you-never-wrote&quot; aria-label=&quot;Anchor link for: 2-autograd-records-a-program-you-never-wrote&quot;&gt;2. Autograd records a program you never wrote&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 2, Autograd&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;In your code, &lt;code&gt;y&lt;&#x2F;code&gt; is one name, and you overwrite it freely. Line
two destroys the value line one made. The graph cannot afford
that: backward will need every step. So autograd writes one record
per change, and no record is ever overwritten. Watch the record
change as &lt;code&gt;y&lt;&#x2F;code&gt; does:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ones&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; requires_grad&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt;True&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;MulBackward0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;add_&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;                      # change y in place&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;AddBackward0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-keyword z-operator z-assignment&quot;&gt;] =&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 9&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;                       # overwrite one slot&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; type&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;y&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;CopySlices&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-shadow&quot; style=&quot;--dw: 648px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-shadow-light.svg?h=ee8ea6d650a2aa8bb204&quot;
         alt=&quot;left, what your code sees: three code cards named y equals a times 2, y dot add underscore 1, and y bracket 0 equals 9; the first two are struck through because each line replaced the old value; right, what the graph kept: three records named MulBackward0, AddBackward0 and CopySlices, chained by upward arrows, with y dot grad underscore fn entering at the newest; a dashed thread ties each code line to its record&quot; width=&quot;648&quot; height=&quot;350&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-shadow-dark.svg?h=b87d476e2fab59934ca7&quot;
         alt=&quot;left, what your code sees: three code cards named y equals a times 2, y dot add underscore 1, and y bracket 0 equals 9; the first two are struck through because each line replaced the old value; right, what the graph kept: three records named MulBackward0, AddBackward0 and CopySlices, chained by upward arrows, with y dot grad underscore fn entering at the newest; a dashed thread ties each code line to its record&quot; width=&quot;648&quot; height=&quot;350&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 19.&lt;&#x2F;span&gt; your code keeps one value and destroys the past. the graph keeps every step: one record per change, nothing overwritten.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Three statements, three records, one chain. &lt;code&gt;y.grad_fn&lt;&#x2F;code&gt; always
holds the newest record, and each record points at the one before
it, so the whole history stays reachable. That history is the
program you never wrote. The machinery that keeps it correct under
every kind of in-place change has real depth, and it is one of the
best chapters of Part 2.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;3-one-list-of-operations-is-the-whole-interface&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#3-one-list-of-operations-is-the-whole-interface&quot; aria-label=&quot;Anchor link for: 3-one-list-of-operations-is-the-whole-interface&quot;&gt;3. One list of operations is the whole interface&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 5, The Machinery&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-isa&quot; style=&quot;--dw: 652px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-isa-light.svg?h=a94ed872072b10ed60f9&quot;
         alt=&quot;a code panel with the lines h equals x at w, h equals relu of h, loss equals h dot sum; an orange arrow labeled becomes points to three tiles named matmul, relu and sum, marked as the list with 3,677 possible names; a bracket collects the three tiles and fans out to three boxes named cpu, cuda and quantized&quot; width=&quot;652&quot; height=&quot;313&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-isa-dark.svg?h=3c818aba537b7ddde638&quot;
         alt=&quot;a code panel with the lines h equals x at w, h equals relu of h, loss equals h dot sum; an orange arrow labeled becomes points to three tiles named matmul, relu and sum, marked as the list with 3,677 possible names; a bracket collects the three tiles and fans out to three boxes named cpu, cuda and quantized&quot; width=&quot;652&quot; height=&quot;313&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 20.&lt;&#x2F;span&gt; the code on the left never reaches a device. only the list does.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The 3,677 registered names are PyTorch&#x27;s real interface. Each
backend implements its share of them. The compiler rewrites
programs made of them: &lt;code&gt;torch.compile&lt;&#x2F;code&gt; reads the list your program
became and returns a shorter one, where a matrix multiplication
and the add after it can fuse into one &lt;code&gt;addmm&lt;&#x2F;code&gt;, and a chain of
small elementwise operations becomes one generated kernel. Export
formats store them: &lt;code&gt;torch.export&lt;&#x2F;code&gt; writes the list to disk as a
graph of exactly these names, and an ONNX file is the same idea
with each name translated into ONNX&#x27;s vocabulary. Quantization
replaces them: the float32 matmul is swapped for an int8 body, the
same place on the list, different arithmetic. When you meet a new
PyTorch technology, ask one question first: what does it do to the
operations? The answer usually explains the whole design.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;4-pytorch-writes-most-of-its-own-code&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#4-pytorch-writes-most-of-its-own-code&quot; aria-label=&quot;Anchor link for: 4-pytorch-writes-most-of-its-own-code&quot;&gt;4. PyTorch writes most of its own code&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 5, The Machinery&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-seed&quot; style=&quot;--dw: 492px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-seed-light.svg?h=aab8fc04bd383cc9485e&quot;
         alt=&quot;a file icon labeled native functions yaml with an arrow branching into python bindings, autograd records, dispatcher entries and type stubs&quot; width=&quot;492&quot; height=&quot;265&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-seed-dark.svg?h=ca14a52218afd9cd4fb6&quot;
         alt=&quot;a file icon labeled native functions yaml with an arrow branching into python bindings, autograd records, dispatcher entries and type stubs&quot; width=&quot;492&quot; height=&quot;265&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 21.&lt;&#x2F;span&gt; one yaml file in, thousands of functions out, at every build.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;&lt;code&gt;native_functions.yaml&lt;&#x2F;code&gt; declares every operation.
&lt;code&gt;derivatives.yaml&lt;&#x2F;code&gt; declares every derivative. At build time,
&lt;code&gt;torchgen&#x2F;&lt;&#x2F;code&gt; reads both and writes the Python bindings, the autograd
record classes and the dispatcher tables. This is why searching the
repository for a function you just called can find nothing: you
searched the input of the build, and the function is in the output.
People who work on PyTorch read the yaml first. After Part 5, so
will you.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;5-features-are-layers-with-a-switch&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#5-features-are-layers-with-a-switch&quot; aria-label=&quot;Anchor link for: 5-features-are-layers-with-a-switch&quot;&gt;5. Features are layers with a switch&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 5, The Machinery&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-lenses&quot; style=&quot;--dw: 355px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-lenses-light.svg?h=44974ae6ec8fbf715d51&quot;
         alt=&quot;an orange call arrow passes down through a stack of three layers labeled autograd awake, autocast asleep, vmap asleep, then reaches the kernel&quot; width=&quot;355&quot; height=&quot;340&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-lenses-dark.svg?h=f1bb609942809b7fa3b3&quot;
         alt=&quot;an orange call arrow passes down through a stack of three layers labeled autograd awake, autocast asleep, vmap asleep, then reaches the kernel&quot; width=&quot;355&quot; height=&quot;340&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 22.&lt;&#x2F;span&gt; every feature watches the same stream of operations. a context manager puts one layer to sleep.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;PyTorch&#x27;s features combine cleanly because each one is a layer in
the dispatcher, watching the same stream of operations:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; with&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;no_grad&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;     z&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;grad_fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; is&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-language&quot;&gt; None&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;              # nothing was recorded&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-language&quot;&gt;True&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;no_grad&lt;&#x2F;code&gt; edited no function. It set a flag that sends operations
past the autograd layer, so nothing gets recorded. Mixed precision,
tracing and vmap work the same way, and that is why they can be
combined without knowing about each other. Part 5 opens the
machinery under the flag.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;6-every-operation-pays-a-fixed-cost-first&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#6-every-operation-pays-a-fixed-cost-first&quot; aria-label=&quot;Anchor link for: 6-every-operation-pays-a-fixed-cost-first&quot;&gt;6. Every operation pays a fixed cost first&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 7, The Compiler&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;p&gt;Half a microsecond of crossing and routing before any math, on
every single operation. That was the measurement on the boundary
floor. Applied honestly, this one number explains why
&lt;code&gt;torch.compile&lt;&#x2F;code&gt; exists, why fused optimizers exist, and why the
first question about any slow model is: is it limited by compute,
or by the cost of issuing many small operations?&lt;&#x2F;p&gt;
&lt;div class=&quot;widget&quot; data-widget=&quot;toll-meter&quot;&gt;
  &lt;div class=&quot;w-frame&quot;&gt;
    &lt;span class=&quot;w-tag&quot;&gt;Interactive&lt;&#x2F;span&gt;
    &lt;div class=&quot;w-mount&quot;&gt;
      &lt;img class=&quot;drawing-light&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-toll-light.svg?h=c69181bd88364622e3d5&quot;
           alt=&quot;two time bars: adding one number is a short bar that is almost entirely the orange fixed cost; adding four million numbers is a long bar with the same orange head followed by a long grey stretch of mathematics&quot; width=&quot;697&quot; height=&quot;371&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;img class=&quot;drawing-dark&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-toll-dark.svg?h=e96705a598f6e28db30f&quot;
           alt=&quot;two time bars: adding one number is a short bar that is almost entirely the orange fixed cost; adding four million numbers is a long bar with the same orange head followed by a long grey stretch of mathematics&quot; width=&quot;697&quot; height=&quot;371&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;p class=&quot;w-note&quot;&gt;this instrument needs javascript; the still drawing stands in.&lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
  &lt;&#x2F;div&gt;
  &lt;p class=&quot;w-cap&quot;&gt;&lt;span class=&quot;fig-label&quot;&gt;Interactive 3.&lt;&#x2F;span&gt; the same add at twelve measured sizes. the dots are measured on the author&#x27;s machine; plant your flag before the curve appears.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;h3 id=&quot;7-memory-not-speed-is-what-kills-training-runs&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#7-memory-not-speed-is-what-kills-training-runs&quot; aria-label=&quot;Anchor link for: 7-memory-not-speed-is-what-kills-training-runs&quot;&gt;7. Memory, not speed, is what kills training runs&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 4, Seeing PyTorch&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 372px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-ledger-light.svg?h=87ec03aa46fb7c937fbf&quot;
         alt=&quot;an account book with columns borrowed and repaid at backward, rows for activations, workspace and parameters&quot; width=&quot;372&quot; height=&quot;305&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-ledger-dark.svg?h=6235dc2e80222ff92556&quot;
         alt=&quot;an account book with columns borrowed and repaid at backward, rows for activations, workspace and parameters&quot; width=&quot;372&quot; height=&quot;305&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 23.&lt;&#x2F;span&gt; the forward pass borrows memory. backward repays it. running out is the most common way a training run dies.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;A slow program still finishes. A program that runs out of GPU
memory dies with &lt;code&gt;CUDA out of memory&lt;&#x2F;code&gt;, and that is the most common
death in all of PyTorch. The forward pass saves values for
backward (the turn, above). The allocator keeps and reuses blocks.
Between them they decide how large a model you can train. This
series treats memory as a first-class subject in Part 4.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;8-python-is-why-it-won-and-what-it-costs&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#8-python-is-why-it-won-and-what-it-costs&quot; aria-label=&quot;Anchor link for: 8-python-is-why-it-won-and-what-it-costs&quot;&gt;8. Python is why it won, and what it costs&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 7, The Compiler&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-moat&quot; style=&quot;--dw: 409px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-moat-light.svg?h=eb1c38b5a57765d0413f&quot;
         alt=&quot;a castle labeled python inside a blue ring of water, with one bridge leading out to the words c++ speed&quot; width=&quot;409&quot; height=&quot;301&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-moat-dark.svg?h=482d0a42beb877029d26&quot;
         alt=&quot;a castle labeled python inside a blue ring of water, with one bridge leading out to the words c++ speed&quot; width=&quot;409&quot; height=&quot;301&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 24.&lt;&#x2F;span&gt; the protection and the price are the same picture: everything must cross one bridge.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;PyTorch won because you write it in ordinary Python, with ordinary
debuggers and print statements. The price is the border cost from
Idea 6, paid on every operation. The history of the framework is a
sequence of attempts to keep the first while reducing the second.
TorchScript tried to replace Python with its own language; it is
now in maintenance mode &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-4&quot;&gt;4&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The current compiler watches your
Python run and translates what it can, and it is winning. The
pattern to remember: inside PyTorch, betting against Python has
always lost.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;9-forward-decides-what-backward-must-do&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#9-forward-decides-what-backward-must-do&quot; aria-label=&quot;Anchor link for: 9-forward-decides-what-backward-must-do&quot;&gt;9. Forward decides what backward must do&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 9, Distributed&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-mirror&quot; style=&quot;--dw: 486px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-mirror-light.svg?h=bb40d6a00efe903f3144&quot;
         alt=&quot;a forward chain of boxes x, mul, add, loss above a dashed line, with its orange reflection below running in the opposite direction through the backward records&quot; width=&quot;486&quot; height=&quot;255&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-mirror-dark.svg?h=6176a491457b5eb2c9b9&quot;
         alt=&quot;a forward chain of boxes x, mul, add, loss above a dashed line, with its orange reflection below running in the opposite direction through the backward records&quot; width=&quot;486&quot; height=&quot;255&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 25.&lt;&#x2F;span&gt; backward is the reflection of the graph forward wrote.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Backward can only walk what forward wrote. On one machine this
sounds like a detail. At scale it becomes the law of the land: in
distributed training, the way a tensor is split across GPUs in the
forward pass decides which GPUs must exchange data in the backward
pass. One idea, from a laptop to a cluster. It is the spine of
Part 9.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;10-shared-bytes-plus-in-place-writes-cause-the-hardest-problems&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#10-shared-bytes-plus-in-place-writes-cause-the-hardest-problems&quot; aria-label=&quot;Anchor link for: 10-shared-bytes-plus-in-place-writes-cause-the-hardest-problems&quot;&gt;10. Shared bytes plus in-place writes cause the hardest problems&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 2, Autograd&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-alias&quot; style=&quot;--dw: 424px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-alias-light.svg?h=7230304100dd19a8eb14&quot;
         alt=&quot;one block of storage with three overlapping window frames over it and a single orange write striking a cell that two of the windows share&quot; width=&quot;424&quot; height=&quot;261&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-alias-dark.svg?h=6b130ac532b1dc2d415d&quot;
         alt=&quot;one block of storage with three overlapping window frames over it and a single orange write striking a cell that two of the windows share&quot; width=&quot;424&quot; height=&quot;261&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 26.&lt;&#x2F;span&gt; three windows, one storage, one write. every system that records or rewrites programs must handle this.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Idea 1 lets many tensors read the same bytes. Idea 2 lets you
change those bytes in place. Combine them: one write can change the
meaning of several tensors at once, and any system that records
programs (autograd, the compiler, export) must notice and stay
correct. When a corner of PyTorch looks strangely complicated, ask
what shared bytes plus an in-place write would do to it. That is
usually the answer.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;11-the-code-keeps-its-history&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#11-the-code-keeps-its-history&quot; aria-label=&quot;Anchor link for: 11-the-code-keeps-its-history&quot;&gt;11. The code keeps its history&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 5, The Machinery&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-strata&quot; style=&quot;--dw: 394px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-strata-light.svg?h=bc6a0ebbcd9c665596c0&quot;
         alt=&quot;a cross section of ground with four labeled layers: dynamo and inductor on top, then torchscript, the caffe2 merge, and the original TH C code from 2016 at the bottom&quot; width=&quot;394&quot; height=&quot;315&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-strata-dark.svg?h=be686e84b0b972ab07cf&quot;
         alt=&quot;a cross section of ground with four labeled layers: dynamo and inductor on top, then torchscript, the caffe2 merge, and the original TH C code from 2016 at the bottom&quot; width=&quot;394&quot; height=&quot;315&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 27.&lt;&#x2F;span&gt; four systems, four eras, one repository. older layers still show through.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The repository holds the remains of every era: the original C code
from 2016, the Caffe2 merge of 2018, TorchScript from 2019, the
compiler district growing since 2023. When a file looks strange,
the explanation is usually historical: something older lived there
first. Part 5 tells this history where it explains the present.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;12-floating-point-is-a-contract-read-it&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#12-floating-point-is-a-contract-read-it&quot; aria-label=&quot;Anchor link for: 12-floating-point-is-a-contract-read-it&quot;&gt;12. Floating point is a contract; read it&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;

&lt;aside class=&quot;floor-note pnote&quot;&gt;
  &lt;div class=&quot;fn-text&quot;&gt;&lt;span class=&quot;fn-part&quot;&gt;full story: Part 4, Seeing PyTorch&lt;&#x2F;span&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;aside&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;idea-contract&quot; style=&quot;--dw: 505px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-contract-light.svg?h=3349ef2f6d69fd248b40&quot;
         alt=&quot;a document titled float32 the contract, with a highlighted clause reading 1e8 plus 1 minus 1e8 equals 0, signed by every model you train&quot; width=&quot;505&quot; height=&quot;332&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;idea-contract-dark.svg?h=6e5d7cd404a4144c66e4&quot;
         alt=&quot;a document titled float32 the contract, with a highlighted clause reading 1e8 plus 1 minus 1e8 equals 0, signed by every model you train&quot; width=&quot;505&quot; height=&quot;332&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 28.&lt;&#x2F;span&gt; the terms are public. every training run signs them.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tensor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1e8&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; ((&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;item&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A float32 number has about 7 decimal digits of precision, so
adding 1 to one hundred million changes nothing &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-5&quot;&gt;5&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. This is not a
bug; it is the number format doing what it promises. Add the
faster, less precise formats used in training, plus the fact that
some GPU kernels sum in different orders on different runs, and
&quot;why did my loss change between runs&quot; becomes a question with exact
answers. Part 4 reads this contract clause by clause.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;how-this-series-draws&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-this-series-draws&quot; aria-label=&quot;Anchor link for: how-this-series-draws&quot;&gt;How this series draws&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Every still figure you just saw is a real Excalidraw scene, and the
scene files ship with the series; you can open any drawing and edit
it. The four instruments you can operate follow the same language,
and every number inside them comes from the proof scripts.
All of them speak one visual language, so that by Part 2 you read
them without thinking:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 759px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;legend-light.svg?h=2b4838282afb188134ff&quot;
         alt=&quot;a legend sheet: an orange arrow meaning the subject in motion, an ink rectangle meaning structure, a grey panel meaning context, a dashed line meaning implied or asleep, the depth meter, and the robot with a note that it appears at most once per part&quot; width=&quot;759&quot; height=&quot;356&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;legend-dark.svg?h=a02f0f6a720fd32fe7cd&quot;
         alt=&quot;a legend sheet: an orange arrow meaning the subject in motion, an ink rectangle meaning structure, a grey panel meaning context, a dashed line meaning implied or asleep, the depth meter, and the robot with a note that it appears at most once per part&quot; width=&quot;759&quot; height=&quot;356&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 29.&lt;&#x2F;span&gt; the whole notation on one sheet. learn it once; it holds for the entire series.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Orange always marks the subject: the one thing moving. Ink is
structure. Grey is context. Dashed means recorded, implied, or
asleep. The depth meter marks the floor. And the robot appears at
most once per part, because a mascot that is everywhere stops
being funny.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;how-to-read-this&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#how-to-read-this&quot; aria-label=&quot;Anchor link for: how-to-read-this&quot;&gt;How to read this&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Twelve parts. Each is one long page like this one. And the series
has one quiet goal behind every part: by the end, you should know
the machine well enough to build a small PyTorch yourself. Every
drawing that shows a mechanism, every formula next to a figure, and
every proof script is a piece of that.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Part&lt;&#x2F;th&gt;&lt;th&gt;What is behind the door&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;0. The Map&lt;&#x2F;td&gt;&lt;td&gt;you are here&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;1. Tensor&lt;&#x2F;td&gt;&lt;td&gt;storage, strides, views, data types, broadcasting&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;2. Autograd&lt;&#x2F;td&gt;&lt;td&gt;the graph, in-place writes, checkpointing, double backward&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;3. Daily PyTorch&lt;&#x2F;td&gt;&lt;td&gt;nn, optim, data loading, mixed precision, seen from inside&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;4. Seeing PyTorch&lt;&#x2F;td&gt;&lt;td&gt;the profiler, memory, floating point, honest measurement&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;5. The Machinery&lt;&#x2F;td&gt;&lt;td&gt;the dispatcher, aten, torchgen, the history&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;6. Extending PyTorch&lt;&#x2F;td&gt;&lt;td&gt;subclasses, custom operations, new backends&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;7. The Compiler&lt;&#x2F;td&gt;&lt;td&gt;dynamo, aot autograd, inductor, dynamic shapes&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;8. Kernels &amp;amp; Hardware&lt;&#x2F;td&gt;&lt;td&gt;the gpu model, triton, cutlass, what fast means&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;9. Distributed&lt;&#x2F;td&gt;&lt;td&gt;collectives, ddp, fsdp, dtensor, parallel training&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;10. Ship It&lt;&#x2F;td&gt;&lt;td&gt;export, quantization, executorch, the ecosystem&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;11. Working on PyTorch&lt;&#x2F;td&gt;&lt;td&gt;the contributor&#x27;s field guide&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;You do not have to read front to back. Three reading lines run
through the parts, like lines through stations:&lt;&#x2F;p&gt;
&lt;div class=&quot;widget&quot; data-widget=&quot;metro&quot;&gt;
  &lt;div class=&quot;w-frame&quot;&gt;
    &lt;span class=&quot;w-tag&quot;&gt;Interactive&lt;&#x2F;span&gt;
    &lt;div class=&quot;w-mount&quot;&gt;
      &lt;img class=&quot;drawing-light&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;trails-metro-light.svg?h=6ebfa59198bfeb87bd79&quot;
           alt=&quot;the twelve parts as capsule stations in two rows with four lines running through them on separate tracks: grey front to back, orange ml engineer, blue contributor, green performance; each stop is a dot of the line&amp;#x27;s own color on its own track&quot; width=&quot;641&quot; height=&quot;364&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;img class=&quot;drawing-dark&quot;
           src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;trails-metro-dark.svg?h=b782b2e245e0ad8a5fd9&quot;
           alt=&quot;the twelve parts as capsule stations in two rows with four lines running through them on separate tracks: grey front to back, orange ml engineer, blue contributor, green performance; each stop is a dot of the line&amp;#x27;s own color on its own track&quot; width=&quot;641&quot; height=&quot;364&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
      &lt;p class=&quot;w-note&quot;&gt;this instrument needs javascript; the still drawing stands in.&lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
  &lt;&#x2F;div&gt;
  &lt;p class=&quot;w-cap&quot;&gt;&lt;span class=&quot;fig-label&quot;&gt;Interactive 4.&lt;&#x2F;span&gt; pick a line. grey is front to back and stops everywhere; orange fits most readers; green chases speed; blue is the one for a new contributor. a dot means the line stops there.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;p&gt;Every chapter inside every part follows the same seven steps, so
the rhythm becomes familiar fast:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; data-anim=&quot;contract-anatomy&quot; style=&quot;--dw: 600px;&quot;&gt;
  &lt;div class=&quot;drawing-card&quot;&gt;
    &lt;img class=&quot;drawing-light&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;contract-anatomy-light.svg?h=2fd74439b2d01ee97a8d&quot;
         alt=&quot;a tall page outline with seven stacked bands labeled the question, the model, the mechanism, the source, the proof, the payoff, the frontier, with the proof band highlighted in orange&quot; width=&quot;600&quot; height=&quot;592&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
    &lt;img class=&quot;drawing-dark&quot;
         src=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;ldr&#x2F;ch00&#x2F;drawings&#x2F;contract-anatomy-dark.svg?h=432f6a46bdc643968fa7&quot;
         alt=&quot;a tall page outline with seven stacked bands labeled the question, the model, the mechanism, the source, the proof, the payoff, the frontier, with the proof band highlighted in orange&quot; width=&quot;600&quot; height=&quot;592&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;
  &lt;&#x2F;div&gt;
  &lt;figcaption&gt;&lt;span class=&quot;fig-label&quot;&gt;Figure 30.&lt;&#x2F;span&gt; the seven steps of every chapter. the proof step is the spine: no claim without a script.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;And the method, stated plainly, because you should know what you
are trusting. Every mechanism claim is checked against the source
code or shown by a script before it is published. The scripts are
linked in place and pinned to one torch version. When PyTorch moves
and a claim goes stale, the chapter is corrected and the correction
is noted on the page. A series about internals that cannot admit
drift would be wrong within a year.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-you-can-now-say&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-you-can-now-say&quot; aria-label=&quot;Anchor link for: what-you-can-now-say&quot;&gt;What you can now say&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Test yourself against this list. After one reading you should be
able to say, in your own words:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;what &lt;code&gt;type(torch.randn)&lt;&#x2F;code&gt; returns, and where the compiled body
actually lives on your disk&lt;&#x2F;li&gt;
&lt;li&gt;what the dispatcher is, and how &lt;code&gt;no_grad&lt;&#x2F;code&gt; stops autograd
without editing any function&lt;&#x2F;li&gt;
&lt;li&gt;what a kernel is, and what decides which one runs&lt;&#x2F;li&gt;
&lt;li&gt;why the CPU and the GPU run on two clocks, and why that makes
simple timing code lie&lt;&#x2F;li&gt;
&lt;li&gt;what the graph is, who writes it, and why backward can never do
anything forward did not write down&lt;&#x2F;li&gt;
&lt;li&gt;and the twelve ideas, each in one sentence&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;If one of these is fuzzy, return to its floor; each one
is only a minute long. That is what this page is for.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;try-it-yourself&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#try-it-yourself&quot; aria-label=&quot;Anchor link for: try-it-yourself&quot;&gt;Try it yourself&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The five proof scripts are the exercises. For each one: predict
the output first, then run it, then explain the difference.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p0-the-library&quot;&gt;p0_the_library.py&lt;&#x2F;a&gt;: how
big are the compiled libraries in your own torch install?&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p1-graph-chain&quot;&gt;p1_graph_chain.py&lt;&#x2F;a&gt;: what
graph remains after a two-layer model runs?&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p2-dispatch-cost&quot;&gt;p2_dispatch_cost.py&lt;&#x2F;a&gt;:
what is the fixed cost per operation on your machine?&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p3-two-timelines&quot;&gt;p3_two_timelines.py&lt;&#x2F;a&gt;:
how long is your GPU still working after Python is done asking?&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;part-0-the-map&#x2F;#proof-p4-micro-proofs&quot;&gt;p4_micro_proofs.py&lt;&#x2F;a&gt;: the
twelve ideas, compressed into five small experiments.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h2 id=&quot;pick-a-door&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#pick-a-door&quot; aria-label=&quot;Anchor link for: pick-a-door&quot;&gt;Pick a door&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Part 1 is the tensor. It opens with the puzzle from Idea 1, and now
you have seen the error with your own eyes:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo z-code&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;view&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-support&quot;&gt;RuntimeError&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; view size&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; is not&lt;&#x2F;span&gt;&lt;span&gt; compatible&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; with&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; input&lt;&#x2F;span&gt;&lt;span&gt; tensor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;s&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;size&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; and&lt;&#x2F;span&gt;&lt;span&gt; stride&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; ...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;reshape&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;   # this one works. why?&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tensor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;([&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 4&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Same tensor. Same request. One line refuses, the other quietly
copies the data. The difference between those two lines is the
whole first part of this series.&lt;&#x2F;p&gt;
&lt;p&gt;See you on the next floor down.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;references&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#references&quot; aria-label=&quot;Anchor link for: references&quot;&gt;References&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-1&quot;&gt;[1]&lt;&#x2F;span&gt; Khalilli, &lt;em&gt;five proof scripts, measured on an Apple M3 Max,
torch 2.11.0, CPU and Apple GPU&lt;&#x2F;em&gt;, 2026. Linked in place above;
rerun them to check me.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-2&quot;&gt;[2]&lt;&#x2F;span&gt; PyTorch source, &lt;em&gt;native_functions.yaml&lt;&#x2F;em&gt;, pinned to the v2.11.0
tag. &lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pytorch&#x2F;pytorch&#x2F;blob&#x2F;v2.11.0&#x2F;aten&#x2F;src&#x2F;ATen&#x2F;native&#x2F;native_functions.yaml&quot;&gt;https:&#x2F;&#x2F;github.com&#x2F;pytorch&#x2F;pytorch&#x2F;blob&#x2F;v2.11.0&#x2F;aten&#x2F;src&#x2F;ATen&#x2F;native&#x2F;native_functions.yaml&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-3&quot;&gt;[3]&lt;&#x2F;span&gt; PyTorch source, &lt;em&gt;derivatives.yaml&lt;&#x2F;em&gt;, pinned to the v2.11.0 tag.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pytorch&#x2F;pytorch&#x2F;blob&#x2F;v2.11.0&#x2F;tools&#x2F;autograd&#x2F;derivatives.yaml&quot;&gt;https:&#x2F;&#x2F;github.com&#x2F;pytorch&#x2F;pytorch&#x2F;blob&#x2F;v2.11.0&#x2F;tools&#x2F;autograd&#x2F;derivatives.yaml&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-4&quot;&gt;[4]&lt;&#x2F;span&gt; PyTorch documentation, &lt;em&gt;TorchScript&lt;&#x2F;em&gt;, which states it is in
maintenance mode. &lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;docs.pytorch.org&#x2F;docs&#x2F;stable&#x2F;jit.html&quot;&gt;https:&#x2F;&#x2F;docs.pytorch.org&#x2F;docs&#x2F;stable&#x2F;jit.html&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-5&quot;&gt;[5]&lt;&#x2F;span&gt; IEEE, &lt;em&gt;754 single precision&lt;&#x2F;em&gt;: 24 binary digits of precision,
about 7 decimal digits.&lt;&#x2F;p&gt;
&lt;p&gt;Three good things to read after this page: Edward Yang&#x27;s
&lt;a rel=&quot;noopener nofollow noreferrer external&quot; target=&quot;_blank&quot; href=&quot;http:&#x2F;&#x2F;blog.ezyang.com&#x2F;2019&#x2F;05&#x2F;pytorch-internals&#x2F;&quot;&gt;PyTorch internals talk&lt;&#x2F;a&gt;,
which maps the C++ side in depth; the
&lt;a rel=&quot;noopener nofollow noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;pytorch-dev-podcast.simplecast.com&#x2F;&quot;&gt;PyTorch Developer Podcast&lt;&#x2F;a&gt;,
short episodes by the same author; and the repository&#x27;s own
&lt;a rel=&quot;noopener nofollow noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;pytorch&#x2F;pytorch&#x2F;blob&#x2F;v2.11.0&#x2F;CONTRIBUTING.md&quot;&gt;CONTRIBUTING.md&lt;&#x2F;a&gt;,
which describes the folder layout in the maintainers&#x27; words.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
