<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>tensor</title>
    <subtitle>PyTorch, from the API to the silicon</subtitle>
    <link rel="self" type="application/atom+xml" href="https://tensor.khalilli.ai/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://tensor.khalilli.ai"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-08-10T00:00:00+00:00</updated>
    <id>https://tensor.khalilli.ai/atom.xml</id>
    <entry xml:lang="en">
        <title>Floating Point</title>
        <published>2026-08-10T00:00:00+00:00</published>
        <updated>2026-08-10T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              alikhalilli
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://tensor.khalilli.ai/blog/floating-point/"/>
        <id>https://tensor.khalilli.ai/blog/floating-point/</id>
        
        <content type="html" xml:base="https://tensor.khalilli.ai/blog/floating-point/">&lt;p&gt;Run this anywhere:&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-constant z-numeric&quot;&gt; 0.1&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; 0.2&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.30000000000000004&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-constant z-numeric&quot;&gt; 0.1&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; 0.2&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; 0.3&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;False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And in real training runs, this happens:&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;step 39,998   loss 2.41&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;step 39,999   loss 2.40&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;step 40,000   loss nan&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both outputs have the same cause: the way computers store numbers.
By the end of this page you will be able to predict both, explain
them to someone else, and read a line like &quot;MXFP4, E2M1, block 32,
E8M0 scale&quot; part by part. No prior knowledge is assumed. Every measured number on this page comes
from a small script you can run, linked where the number appears,
measured on an Apple M3 Max 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;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-fixed-set-of-points&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#a-fixed-set-of-points&quot; aria-label=&quot;Anchor link for: a-fixed-set-of-points&quot;&gt;A fixed set of points&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;A computer stores a number in a fixed number of bits. Each bit
has 2 states, so 32 bits can form 2 x 2 x ... x 2 = 2^32 =
4,294,967,296 different patterns. A number format assigns one
number to each pattern. So for a 32-bit machine only those
4,294,967,296 numbers exist; anything else you compute is moved
to the nearest one of them, and the distance moved is the
rounding error. A format is exactly this: a choice of which
numbers get a pattern. This page is about how that choice is
made.&lt;&#x2F;p&gt;
&lt;p&gt;First, see the size of the problem. Between 1 and 2 there are
infinitely many numbers. Zoom in anywhere and you find more.
The line is never empty, at any depth. A format is a finite
list of points. So almost no computed number is on the list,
and almost every operation ends with a move to the nearest
point:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 748px;&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;chfp&#x2F;drawings&#x2F;infinitely-many-light.svg?h=5d389731effc1afb2549&quot;
         alt=&quot;the stretch of line from 1 to 2 with a few of its numbers marked in blue: 1.1, 1.41421..., 1.5, 1.61803..., 1.9999; a zoom into the sliver between 1.41 and 1.42 shows more numbers again, with the note zoom anywhere, forever: never empty; below, the same stretch as an 8-bit format sees it: 8 orange points and nothing else, and every other number must move to one of them&quot; width=&quot;748&quot; height=&quot;351&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;chfp&#x2F;drawings&#x2F;infinitely-many-dark.svg?h=0d9ed26c3e06b19c2930&quot;
         alt=&quot;the stretch of line from 1 to 2 with a few of its numbers marked in blue: 1.1, 1.41421..., 1.5, 1.61803..., 1.9999; a zoom into the sliver between 1.41 and 1.42 shows more numbers again, with the note zoom anywhere, forever: never empty; below, the same stretch as an 8-bit format sees it: 8 orange points and nothing else, and every other number must move to one of them&quot; width=&quot;748&quot; height=&quot;351&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; infinitely many numbers on the line, 8 points in the format. every number off the list moves to the nearest point.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The simplest choice is equal spacing: 0, 1, 2, 3, and so on, the
same distance 1 between every pair of neighbors. That is what
integers are, and for counting it works. Now take two numbers
that one real neural network produced in one training step: an
activation of 10.5 (a value flowing through the network) and a
gradient of 0.0000004 (a value that steers learning). Store both
with equally spaced points:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 722px;&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;chfp&#x2F;drawings&#x2F;uniform-grid-light.svg?h=4289c9cdd3845d61126b&quot;
         alt=&quot;a number line of equally spaced points with 2 orange arrows landing on it, one at 10.5 and one at 0.0000004; below, 2 magnified windows: the window from 10 to 11 shows 10.5 exactly between the 2 points with a snap arrow to 10 and the error drawn as a thick orange segment, 0.5 over 10.5 equals 4.8 percent; the window from 0 to 1 shows 0.0000004 still touching 0 even magnified, the error the whole value, 100 percent&quot; width=&quot;722&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;chfp&#x2F;drawings&#x2F;uniform-grid-dark.svg?h=41c495e57bd737a4290d&quot;
         alt=&quot;a number line of equally spaced points with 2 orange arrows landing on it, one at 10.5 and one at 0.0000004; below, 2 magnified windows: the window from 10 to 11 shows 10.5 exactly between the 2 points with a snap arrow to 10 and the error drawn as a thick orange segment, 0.5 over 10.5 equals 4.8 percent; the window from 0 to 1 shows 0.0000004 still touching 0 even magnified, the error the whole value, 100 percent&quot; width=&quot;722&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 2.&lt;&#x2F;span&gt; equally spaced points meet 2 real numbers. 10.5 keeps a 4.8% error; 0.0000004 becomes 0, a 100% error.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Those 2 numbers are not invented. They come from 1 training step
of a small network, and this page keeps returning to that step,
so here it is, drawn:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 748px;&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;chfp&#x2F;drawings&#x2F;nn-one-step-light.svg?h=6c58b9b63694d39f88f3&quot;
         alt=&quot;one training step drawn as a real network: 3 columns of neuron squares joined by grey weight edges, a loss box at the right; a blue forward arrow above carries the activations; the edges are the weights, 0.00000125 to 0.3; a dashed orange backward lane below carries 1 gradient per weight, computed from the loss; orange dashed update arrows rise from the lane into the edges; at the bottom, the same step&amp;#x27;s numbers on one log axis: a blue activations bar from 0.000045 to 10.5 above an orange gradients bar from 0.00000037 to 0.71, the backward bar reaching 2 decades farther left&quot; width=&quot;748&quot; height=&quot;546&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;chfp&#x2F;drawings&#x2F;nn-one-step-dark.svg?h=bdb1b158df2c13963335&quot;
         alt=&quot;one training step drawn as a real network: 3 columns of neuron squares joined by grey weight edges, a loss box at the right; a blue forward arrow above carries the activations; the edges are the weights, 0.00000125 to 0.3; a dashed orange backward lane below carries 1 gradient per weight, computed from the loss; orange dashed update arrows rise from the lane into the edges; at the bottom, the same step&amp;#x27;s numbers on one log axis: a blue activations bar from 0.000045 to 10.5 above an orange gradients bar from 0.00000037 to 0.71, the backward bar reaching 2 decades farther left&quot; width=&quot;748&quot; height=&quot;546&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; one training step of the measured network, drawn as the network. the bottom axis carries the step&#x27;s 2 flows on one scale: the backward numbers live far lower than the forward ones.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;3 kinds of numbers move in that step. Activations are the values
flowing forward through the layers. Weights are the numbers the
network is learning; multiplying by them is what a layer does.
Gradients are computed backward from the loss, 1 for every
weight: each one says how much that weight should move. Train
the network for a few seconds and measure all 3:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 746px;&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;chfp&#x2F;drawings&#x2F;where-numbers-live-light.svg?h=eea19a10639322bc4c0b&quot;
         alt=&quot;3 rows on one shared axis with decade gridlines: gradients from 0.00000037 to 0.71, weights from 0.00000125 to 0.3, activations from 0.000045 to 10.5; each row shows its full range as a thin line, the middle 99.8 percent as a box, and its median as a thick mark; a bracket over the gradients computes 0.71 over 0.00000037 as 1,900,000, about 2 to the power 20.9; an arrow under the axis computes 10.5 over 0.00000037 as 28,000,000; a warm band on the axis marks where float16&amp;#x27;s normal points will sit, from 0.000061&quot; width=&quot;746&quot; height=&quot;349&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;chfp&#x2F;drawings&#x2F;where-numbers-live-dark.svg?h=6d8eb9c31d5cf7a8196a&quot;
         alt=&quot;3 rows on one shared axis with decade gridlines: gradients from 0.00000037 to 0.71, weights from 0.00000125 to 0.3, activations from 0.000045 to 10.5; each row shows its full range as a thin line, the middle 99.8 percent as a box, and its median as a thick mark; a bracket over the gradients computes 0.71 over 0.00000037 as 1,900,000, about 2 to the power 20.9; an arrow under the axis computes 10.5 over 0.00000037 as 28,000,000; a warm band on the axis marks where float16&amp;#x27;s normal points will sit, from 0.000061&quot; width=&quot;746&quot; height=&quot;349&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 measured ranges of one small network&#x27;s numbers in one training step. thin line: full range. box: the middle 99.8%. thick mark: the median.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p1-where-numbers-live&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_where_numbers_live.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 a real network&amp;#39;s numbers live. Train a small MLP for&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;two seconds, then measure the spans of its weights, activations, and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;gradients in one step. The same model needs numbers nine orders of&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;magnitude apart; a uniform grid cannot serve both ends.&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;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;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;net&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;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; 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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&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; 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; 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;opt&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;optim&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Adam&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;net&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;parameters&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(),&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; lr&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;1e-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&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;4096&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&gt;; y&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;X&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&gt;prod&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-variable&quot;&gt; keepdim&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;span class=&quot;z-keyword z-operator&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;span class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; step&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;300&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;    opt&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;zero_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;    loss&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;net&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 class=&quot;z-keyword z-operator&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 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;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;mean&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-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;backward&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;; opt&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;step&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;acts&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&gt;h&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tensors&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment 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;weights&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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;cat&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&gt;detach&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;flatten&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; p&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; net&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;parameters&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;grads&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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;cat&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&gt;grad&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;flatten&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; p&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; net&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;parameters&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;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;    a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; net&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;net&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;X&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;    tensors&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;activations&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 z-keyword z-operator z-assignment&quot;&gt;] =&lt;&#x2F;span&gt;&lt;span&gt; net&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&gt;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;flatten&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;final loss &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;item&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;:.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;&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; name&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&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; tensors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;items&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;    nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;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; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;abs&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;    q&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;quantile&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;nz&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;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.001&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.5&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.999&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;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:12s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; |min..max| &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;min&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;:.2e&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; .. &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&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;:.2e&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;p0.1&#x2F;median&#x2F;p99.9  &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;q&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-storage z-type&quot;&gt;:.2e&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F; &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;q&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-storage z-type&quot;&gt;:.2e&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F; &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;q&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-storage z-type&quot;&gt;:.2e&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&gt;span&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; tensors&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;grads&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;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()[&lt;&#x2F;span&gt;&lt;span&gt;tensors&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;grads&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-keyword z-operator&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 class=&quot;z-keyword&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; math&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;gradient span: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;span&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&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; span&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;min&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;:.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; powers of two &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;in one tensor family, this small a net, this early&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-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;decade histogram (percent of values per log10 decade; &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 class=&quot;z-punctuation z-definition z-string&quot;&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;the drawn silhouettes):&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;edges&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; list&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&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-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;8&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-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; name&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&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; tensors&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;items&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;    nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;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; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;log10&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;    counts&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-punctuation&quot;&gt; = [(&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;100.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; ((&lt;&#x2F;span&gt;&lt;span&gt;nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span&gt; a&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; &amp;amp;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&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; 1&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;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;numel&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; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; edges&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;    row&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-punctuation z-definition z-string&quot;&gt; &amp;quot;  &amp;quot;&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 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;c&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:4.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;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; c&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; counts&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;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:12s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; [&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;edges&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&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;..&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;edges&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-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-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;]  &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;row&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p1_where_numbers_live.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 two computed lines in the drawing. Why divide? The
ratio says how many of the small number fit into the big one.
With equal spacing, that is the number of steps between them.
The gradients alone run from 0.00000037 to 0.71, and
0.71 &#x2F; 0.00000037 is about 1,900,000, which is 2^20.9. So if
the spacing is fine enough to keep the smallest gradient, the
largest one sits 1,900,000 steps away. Across kinds it is
worse: 10.5 &#x2F; 0.00000037 = 28,000,000 steps. A larger model
makes both numbers bigger. Equal spacing cannot hold both
ends. Set the spacing to 0.0000004 so the smallest gradient
survives, and even 2^31 = 2,147,483,648 points in the positive
direction reach only 0.0000004 x 2,147,483,648 = 859: the format
ends before 1,000. Set the spacing to 1 so that large values
fit, and every gradient becomes 0, as the first drawing showed.
The spacing itself has to change with the size of the number:
points packed close near zero, spread out far from it.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;floating-the-point&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#floating-the-point&quot; aria-label=&quot;Anchor link for: floating-the-point&quot;&gt;Floating the point&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;One small tool first: what a dot means in base two, because from
here on every number is bits. In decimal, each place is worth a
tenth of the place before it: the 7 in 204.75 means seven tenths.
Binary uses the same rule with 2 in place of 10. Left of the dot
the places are worth 8, 4, 2, 1; right of it they are worth a
half, a quarter, an eighth:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 580px;&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;chfp&#x2F;drawings&#x2F;binary-fractions-light.svg?h=75d89e6d132af6e069f6&quot;
         alt=&quot;2 place value strips: the decimal number 204.75 over its places hundred ten one tenth hundredth, and the binary byte 0010.1100 over its places eight four two one, then one half, one quarter, one eighth, one sixteenth; the bits that are 1 are highlighted and their places sum to 2 plus one half plus one quarter equals 2.75; under each binary place a bar drawn to scale shows its worth halving toward nothing&quot; width=&quot;580&quot; height=&quot;347&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;chfp&#x2F;drawings&#x2F;binary-fractions-dark.svg?h=d5e6a066321b2f375e06&quot;
         alt=&quot;2 place value strips: the decimal number 204.75 over its places hundred ten one tenth hundredth, and the binary byte 0010.1100 over its places eight four two one, then one half, one quarter, one eighth, one sixteenth; the bits that are 1 are highlighted and their places sum to 2 plus one half plus one quarter equals 2.75; under each binary place a bar drawn to scale shows its worth halving toward nothing&quot; width=&quot;580&quot; height=&quot;347&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; the dot in base two. same reading you have done since childhood, with each place worth half the one before instead of a tenth.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Read 0010.1100 with those place values: the 1s sit on 2, on a
half and on a quarter, and 2 + 0.5 + 0.25 = 2.75. That is all a
string of bits with a dot in it can mean, and you can now read
any of them.&lt;&#x2F;p&gt;
&lt;p&gt;One question hides here, and it decides half of this page:
which fractions can these places write exactly? The places are
halves, quarters, eighths. Any finite sum of them is a fraction
whose denominator is a power of 2: 1&#x2F;2, 3&#x2F;4, 5&#x2F;8. Those end
cleanly. Now take 1&#x2F;3, 1&#x2F;5 or 1&#x2F;10. Their denominators carry a
3 or a 5. No finite sum of halves, quarters and eighths equals
them. Watch both cases run:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 793px;&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;chfp&#x2F;drawings&#x2F;which-fractions-fit-light.svg?h=94bfe6d5926c9d28145d&quot;
         alt=&quot;two panels build fractions from the halving places, one bit per row, the blue fill showing the sum so far; left, 5&amp;#x2F;8: add the half, skip the quarter, add the eighth, and the fill reaches the blue target line exactly on the third row, 5&amp;#x2F;8 equals 0.101, finished in 3 bits; right, 1&amp;#x2F;3: skip the half, add the quarter, skip the eighth, add the sixteenth, skip the thirty-second, and a gap to the red target line is left after every row, forever: 1&amp;#x2F;3 equals 0.010101 repeating; below, the rule: a power-of-2 denominator gets hit exactly, a 3 or a 5 never does, and 1&amp;#x2F;10 carries a 5&quot; width=&quot;793&quot; height=&quot;430&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;chfp&#x2F;drawings&#x2F;which-fractions-fit-dark.svg?h=3238edc0f8013990c165&quot;
         alt=&quot;two panels build fractions from the halving places, one bit per row, the blue fill showing the sum so far; left, 5&amp;#x2F;8: add the half, skip the quarter, add the eighth, and the fill reaches the blue target line exactly on the third row, 5&amp;#x2F;8 equals 0.101, finished in 3 bits; right, 1&amp;#x2F;3: skip the half, add the quarter, skip the eighth, add the sixteenth, skip the thirty-second, and a gap to the red target line is left after every row, forever: 1&amp;#x2F;3 equals 0.010101 repeating; below, the rule: a power-of-2 denominator gets hit exactly, a 3 or a 5 never does, and 1&amp;#x2F;10 carries a 5&quot; width=&quot;793&quot; height=&quot;430&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; building fractions from halves, quarters, eighths. 5&#x2F;8 is hit exactly in 3 bits. 1&#x2F;3 leaves a gap after every row, forever: its bits repeat. 1&#x2F;10 carries a 5 and repeats the same way.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Now a first attempt that fails. Take 1 byte, which is 8 bits,
and fix the dot in the middle: 4 bits for the whole part, 4 for
the fraction:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 937px;&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;chfp&#x2F;drawings&#x2F;fixed-dot-light.svg?h=cd79330be2ecd5ce120c&quot;
         alt=&quot;the same 8 bits with the dot fixed in 3 places, and for each choice a bar on one shared log axis from its smallest step to its ceiling: 0.0156 to 3.98, 0.0625 to 15.94, 0.25 to 63.75; the 3 bars are the same length, 255 steps each, and slide right as the dot moves; 2 dashed orange lines mark 0.0000004, left of every bar, and 10.5, covered only by the lower bars&quot; width=&quot;937&quot; height=&quot;390&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;chfp&#x2F;drawings&#x2F;fixed-dot-dark.svg?h=205e34e4ddeb8c6ab858&quot;
         alt=&quot;the same 8 bits with the dot fixed in 3 places, and for each choice a bar on one shared log axis from its smallest step to its ceiling: 0.0156 to 3.98, 0.0625 to 15.94, 0.25 to 63.75; the 3 bars are the same length, 255 steps each, and slide right as the dot moves; 2 dashed orange lines mark 0.0000004, left of every bar, and 10.5, covered only by the lower bars&quot; width=&quot;937&quot; height=&quot;390&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; fixed point: the same 8 bits, 3 dot positions. each choice reaches the same 255 steps; moving the dot slides the reach, never widens it, and no position covers both target numbers.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;This is called fixed point, and the drawing shows its problem:
the dot&#x27;s position decides the largest value and the smallest
step at the same time, and no single position makes both good. The
repair is in the name of this page. Let the dot move, and store
where it went.&lt;&#x2F;p&gt;
&lt;p&gt;Where should the machine write down &quot;where the dot went&quot;? You
already know the answer, because you already do this on paper.
To write a very small or a very large number, you split it into
its digits and its size: 0.00000037 becomes 3.7 x 10^-7. The 3.7
says what the digits are; the 10^-7 says how big the number is.
2 separate jobs, written separately:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 678px;&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;chfp&#x2F;drawings&#x2F;two-jobs-light.svg?h=f0d6d2b2be3027b3eef3&quot;
         alt=&quot;2 bands: the decimal split the reader knows, 0.00000037 equals 3.7 times 10 to the -7 and 10.5 equals 1.05 times 10 to the 1, with the digits chip in blue and the size chip in orange; the same 2 numbers split in base 2, 2 to the -22 times 1.552 and 2 to the 3 times 1.3125; below, the 3 bit fields with dashed arrows from the chips: the orange exponent takes the size, -22 plus 127 equals 105 goes in, the blue mantissa takes the digits .552 in binary places&quot; width=&quot;678&quot; height=&quot;455&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;chfp&#x2F;drawings&#x2F;two-jobs-dark.svg?h=41202f0bc36ab67f439b&quot;
         alt=&quot;2 bands: the decimal split the reader knows, 0.00000037 equals 3.7 times 10 to the -7 and 10.5 equals 1.05 times 10 to the 1, with the digits chip in blue and the size chip in orange; the same 2 numbers split in base 2, 2 to the -22 times 1.552 and 2 to the 3 times 1.3125; below, the 3 bit fields with dashed arrows from the chips: the orange exponent takes the size, -22 plus 127 equals 105 goes in, the blue mantissa takes the digits .552 in binary places&quot; width=&quot;678&quot; height=&quot;455&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; the heart of the format: the digits-and-size split you already write, moved to base 2, one bit field per half.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;A float makes exactly this split, in base 2. Any positive number
is some power of 2 times a number between 1 and 2: 10.5 is
1.3125 x 2^3 (check it: 2^3 = 8, and 1.3125 x 8 = 10.5), and
0.00000037 is about 1.552 x 2^-22. The power of 2 goes into one
field, named the &lt;strong&gt;exponent&lt;&#x2F;strong&gt;. The 1.something goes into the
other field, named the &lt;strong&gt;mantissa&lt;&#x2F;strong&gt;. The 2 words are only names
for the 2 halves of the split: the exponent is the number&#x27;s
size, the mantissa is its digits.&lt;&#x2F;p&gt;
&lt;p&gt;What does each field buy on the number line? Draw them &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-2&quot;&gt;2&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 824px;&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;chfp&#x2F;drawings&#x2F;interval-and-steps-light.svg?h=e3dcc5dc1351e0272789&quot;
         alt=&quot;a number line at true linear scale tiled with doubling intervals from 0.5 to 16, each box twice as wide as the one before, small x2 marks at each border, the interval 8 to 16 in orange with 10.5 landing inside and the exponent&amp;#x27;s stored numbers -1 to 3 written over the boxes; below, that interval magnified and cut into 8 equal steps numbered step 0 to step 7, the points 10 and 11 in orange with 10.5 landing between them, and rounding picks one&quot; width=&quot;824&quot; height=&quot;430&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;chfp&#x2F;drawings&#x2F;interval-and-steps-dark.svg?h=1b9f6183051f8c20a4e9&quot;
         alt=&quot;a number line at true linear scale tiled with doubling intervals from 0.5 to 16, each box twice as wide as the one before, small x2 marks at each border, the interval 8 to 16 in orange with 10.5 landing inside and the exponent&amp;#x27;s stored numbers -1 to 3 written over the boxes; below, that interval magnified and cut into 8 equal steps numbered step 0 to step 7, the points 10 and 11 in orange with 10.5 landing between them, and rounding picks one&quot; width=&quot;824&quot; height=&quot;430&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; what each field does. the exponent counts doubling intervals, drawn at true scale so you can see each one really is twice as wide; the mantissa cuts the chosen interval into 2^M equal steps.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Where do the intervals come from? From the split itself. Fix
the power at 8. The digits part runs from 1.00 to 1.99. So the
values run from 8 to just under 16: every number with power 8
lives between 8 and 16. Fix the power at 16 and you fill 16 to
32. That stretch starts twice as high, so it is twice as wide.
That is all the doubling is. Choosing the exponent is choosing
the interval. 8 exponent bits count 2^8 = 256
intervals, and because every interval doubles, 256 of them span
an enormous distance: that is how 8 bits reach from 1e-38 to
3e38. The mantissa then cuts the chosen interval into equal
steps: 3 bits make 2 x 2 x 2 = 8 steps, binary32&#x27;s 23 bits make
8,388,608. And the growing step we asked for at the start falls
out by itself: [4, 8) is twice as wide as [2, 4), both are cut
into the same number of steps, so every step in [4, 8) is twice
as long. Small numbers get fine steps, large numbers get coarse
ones, with nobody managing it.&lt;&#x2F;p&gt;
&lt;p&gt;Now store one number end to end. Move 1: pick the interval.
6.1 = 1.525 x 4, and 1.525 is between 1 and 2, so the interval
is [4, 8). Move 2: say where inside. 6.1 sits 2.1 past 4, and
2.1 &#x2F; 4 = 0.525. That fraction, 0.525, is what the mantissa
holds:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 859px;&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;chfp&#x2F;drawings&#x2F;floating-dot-light.svg?h=5ec0a69378c42b78160b&quot;
         alt=&quot;two panels: move 1, which interval: a number line from 0 to 8 with the doubling intervals boxed, 6.1&amp;#x27;s arrow landing in the orange interval 4 to 8, because 6.1 equals 1.525 times 4 and 1.525 is between 1 and 2; move 2, where inside: the interval magnified and cut into 8 equal steps of 0.5 labeled 4 through 8, 6.1&amp;#x27;s arrow landing between 6 and 6.5 with the nearest step 6 in orange, and a blue bracket measuring 6.1 sits 2.1 past 4, 2.1 over 4 equals 0.525, the fraction the mantissa field stores; with 3 bits it rounds to 6.0, binary32 cuts the same interval into 8,388,608 steps and lands on 6.0999999&quot; width=&quot;859&quot; height=&quot;447&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;chfp&#x2F;drawings&#x2F;floating-dot-dark.svg?h=960e0d33a1dc646aa870&quot;
         alt=&quot;two panels: move 1, which interval: a number line from 0 to 8 with the doubling intervals boxed, 6.1&amp;#x27;s arrow landing in the orange interval 4 to 8, because 6.1 equals 1.525 times 4 and 1.525 is between 1 and 2; move 2, where inside: the interval magnified and cut into 8 equal steps of 0.5 labeled 4 through 8, 6.1&amp;#x27;s arrow landing between 6 and 6.5 with the nearest step 6 in orange, and a blue bracket measuring 6.1 sits 2.1 past 4, 2.1 over 4 equals 0.525, the fraction the mantissa field stores; with 3 bits it rounds to 6.0, binary32 cuts the same interval into 8,388,608 steps and lands on 6.0999999&quot; width=&quot;859&quot; height=&quot;447&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; storing 6.1 is 2 moves: pick the interval, then cut it into equal steps and take the nearest. the fraction 0.525 is what the mantissa holds.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;In a real 32-bit float the split gets 1 bit for the sign, 8 for
the exponent, 23 for the mantissa. Here is 6.1, every field
decoded:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&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;chfp&#x2F;drawings&#x2F;binary32-card-light.svg?h=7079cc2138fe77336d38&quot;
         alt=&quot;6.1&amp;#x27;s actual 32 bits drawn in their fields: sign 0, exponent 10000001, mantissa 10000110011001100110011; a dashed ghost cell holding 1. above the mantissa marks the hidden 1, never stored; dashed arrows drop from each field to its decode: 10000001 equals 129, and 129 minus 127 equals 2, the interval 4 to 8, with the bias keeping the field unsigned; the mantissa bits read as 0.525, the position; the parts compose into 1 plus 0.525 times 2 squared equals 6.0999999, the nearest float32 to 6.1&quot; width=&quot;737&quot; height=&quot;346&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;chfp&#x2F;drawings&#x2F;binary32-card-dark.svg?h=b99da232e52c966bbc33&quot;
         alt=&quot;6.1&amp;#x27;s actual 32 bits drawn in their fields: sign 0, exponent 10000001, mantissa 10000110011001100110011; a dashed ghost cell holding 1. above the mantissa marks the hidden 1, never stored; dashed arrows drop from each field to its decode: 10000001 equals 129, and 129 minus 127 equals 2, the interval 4 to 8, with the bias keeping the field unsigned; the mantissa bits read as 0.525, the position; the parts compose into 1 plus 0.525 times 2 squared equals 6.0999999, the nearest float32 to 6.1&quot; width=&quot;737&quot; height=&quot;346&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; binary32, decoded by hand: the actual bits of 6.1, the hidden 1 as a ghost cell, and the bias on the exponent&#x27;s path.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Two details in that card deserve their own sentences, because
every format on this page inherits both. First, the decode row
says &quot;1 + 0.525&quot;, but the mantissa field only stored the .525.
The leading 1 costs nothing: every number in a power-of-two
interval is 1.something times a power of two, so the format does
not store the 1 and gets 24 bits of position for the price of 23.
(The subnormals, coming 2 sections from now, are 0.something
and are the one exception.) Second, the &lt;strong&gt;bias&lt;&#x2F;strong&gt;. The exponent field is unsigned: 8 bits,
0 to 255, no minus sign. But interval numbers need minus
signs: 0.00000037 lives at interval -22. The fix is one
constant, 127, the middle of the range. When the machine
stores, it adds 127: the -22 above went in as -22 + 127 = 105.
When it reads, it subtracts 127: this card&#x27;s 129 means
129 - 127 = 2. Same 127, two directions. Every interval number
from -126 to 127 now fits in 1 to 254. (Stored 0 and 255 are
reserved; they build the edge cases 2 sections ahead.)&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&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;chfp&#x2F;drawings&#x2F;bias-two-ways-light.svg?h=7b8a751a0d27d71bd602&quot;
         alt=&quot;two number lines: the interval numbers a format needs, -126 to 127, with -22 and k equals 2 marked in orange; and the 8-bit field&amp;#x27;s codes, 1 to 254, with 105 and 129 marked; a solid orange arrow crosses down labeled storing: add 127, -22 plus 127 equals 105, and a dashed orange arrow crosses back up labeled reading: subtract 127, 129 minus 127 equals 2; below, why 127: it is the middle of the field&amp;#x27;s range, so adding it slides every interval number into 1 to 254 with nothing left over&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;chfp&#x2F;drawings&#x2F;bias-two-ways-dark.svg?h=25762eb3c55eeb4b9000&quot;
         alt=&quot;two number lines: the interval numbers a format needs, -126 to 127, with -22 and k equals 2 marked in orange; and the 8-bit field&amp;#x27;s codes, 1 to 254, with 105 and 129 marked; a solid orange arrow crosses down labeled storing: add 127, -22 plus 127 equals 105, and a dashed orange arrow crosses back up labeled reading: subtract 127, 129 minus 127 equals 2; below, why 127: it is the middle of the field&amp;#x27;s range, so adding it slides every interval number into 1 to 254 with nothing left over&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 12.&lt;&#x2F;span&gt; the bias, both directions: add 127 when storing, subtract 127 when reading. why 127: it is the middle of what 8 unsigned bits can hold.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The unsigned choice has a quiet payoff. A bigger float always
carries a bigger bit pattern, so a chip can compare 2 floats
with the integer circuits it already owns &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Notice what the last decode row admits: the machine does not
store 6.1. It cannot. 6.1 is 61&#x2F;10, and the 10 carries a 5.
The fractions figure showed what a 5 does in base 2: the bits
repeat forever. The mantissa keeps 23 of them and cuts the
rest. What remains decodes to 6.0999999..., the nearest
float32. The gap is the rounding error. The next three
sections are about the size and the consequences of that gap. This layout, the
bias and the field widths, was standardized in 1985 as IEEE 754 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, and it is the
format your &lt;code&gt;float32&lt;&#x2F;code&gt; tensors use today, unchanged.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-map&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-map&quot; aria-label=&quot;Anchor link for: the-map&quot;&gt;The map&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;You cannot draw all 4,294,967,296 points of float32. But the
same design at 8 bits has only 2^8 = 256 patterns, and its
entire positive half fits in one picture. The 8-bit format is
called E4M3, and the name is just its recipe: E4 means 4
exponent bits, M3 means 3 mantissa bits. Every format name on
this page reads the same way. This is the most important drawing on
this page, because every format you will ever meet is this drawing
with different counts:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 854px;&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;chfp&#x2F;drawings&#x2F;e4m3-map-light.svg?h=ea02ea18aa540d821c11&quot;
         alt=&quot;the full positive range of FP8 E4M3 drawn as 16 intervals on one line: a dashed subnormal interval at the far left labeled the ramp to zero, then 15 power of two intervals each holding the same 8 points, a badge counting 127 positive values all drawn, the ramp floor labeled 0.00195, a step chip reading step 32 under the top interval, ending at 448 with a note that there is no infinity and one code means NaN; the interval from 1 to 2 is filled orange and magnified below, its 8 points labeled 1.0 to 1.875 with a step bracket: step 0.125 everywhere in this interval, the next interval 0.25, x2 forever&quot; width=&quot;854&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;chfp&#x2F;drawings&#x2F;e4m3-map-dark.svg?h=6ae74f258bd7aed255e8&quot;
         alt=&quot;the full positive range of FP8 E4M3 drawn as 16 intervals on one line: a dashed subnormal interval at the far left labeled the ramp to zero, then 15 power of two intervals each holding the same 8 points, a badge counting 127 positive values all drawn, the ramp floor labeled 0.00195, a step chip reading step 32 under the top interval, ending at 448 with a note that there is no infinity and one code means NaN; the interval from 1 to 2 is filled orange and magnified below, its 8 points labeled 1.0 to 1.875 with a step bracket: step 0.125 everywhere in this interval, the next interval 0.25, x2 forever&quot; width=&quot;854&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 13.&lt;&#x2F;span&gt; every positive number an FP8 E4M3 can be. each power-of-two interval holds the same 8 points; the step doubles from interval to interval, forever.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p2-e4m3-map&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_e4m3_map.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: every FP8 E4M3 value, decoded from its bits by hand, and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;checked bit-for-bit against torch.float8_e4m3fn. 256 codes: this 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;the entire format, and the decoder is fifteen lines.&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-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; decode_e4m3&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;byte&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;    s&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;byte&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 7&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; &amp;amp;&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;span&gt;    e&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;byte&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;gt;&amp;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-keyword z-operator z-storage z-type&quot;&gt; &amp;amp; 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;F&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    m&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; byte&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt; &amp;amp; 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;7&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; e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt; == 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;F&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; and&lt;&#x2F;span&gt;&lt;span&gt; m&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt; == 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;7&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;        return&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; float&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;nan&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-comment z-comment&quot;&gt;            # the single NaN code per sign&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; e&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; 0&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;                          # subnormal: no hidden 1&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-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;s&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;span class=&quot;z-keyword z-operator&quot;&gt;**&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 7&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-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;m&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; 8&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;    return&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-keyword z-operator&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;s&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;span class=&quot;z-keyword z-operator&quot;&gt;**&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 7&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-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-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; m&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; 8&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;agree&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; values&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&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; byte&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;256&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;    mine&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; decode_e4m3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;byte&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;    ref&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&gt;byte&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;uint8&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float8_e4m3fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&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;    ok&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;mine&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span&gt; mine&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; and&lt;&#x2F;span&gt;&lt;span&gt; ref&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span&gt; ref&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; or&lt;&#x2F;span&gt;&lt;span&gt; mine&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; ref&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    agree&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span&gt; ok&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 class=&quot;z-keyword z-operator&quot;&gt; not&lt;&#x2F;span&gt;&lt;span&gt; ok&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;DISAGREE byte &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;byte&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:08b&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;: mine &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;mine&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;, torch &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;ref&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&gt;    values&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&gt;mine&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;agree&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;&#x2F;256 bit patterns agree with torch.float8_e4m3fn&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;finite&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; sorted&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&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; values&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; v&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pos&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;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; finite&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;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 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;largest value          : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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&gt;finite&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;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;smallest positive      : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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&gt;pos&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;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;positive values        : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&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;pos&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;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;step just above 1.0    : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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&gt;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; pos&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 1.0&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.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;&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;step just below 448    : &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;448&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; max&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&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; pos&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; v&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; 448&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;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;chfp&#x2F;proofs&#x2F;p2_e4m3_map.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 map slowly; every later section builds on it. Every
interval holds exactly 8 points, one per mantissa pattern. Neighbors
inside a interval are 1&#x2F;8 of the interval&#x27;s starting value apart, so
rounding moves a number by at most half of that: 1&#x2F;16 of its own
size, about 6%, and this is the same in every interval, from 0.02 to
400. The step inside [1, 2) is 0.125; the step inside [256, 448]
is 32. So the absolute error grows with the number, but the
relative error does not. Every float format works this way.&lt;&#x2F;p&gt;
&lt;p&gt;The local step also has a proper name, and you will meet it in
every numerics discussion: the &lt;strong&gt;ulp&lt;&#x2F;strong&gt;, the unit in the last
place &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-4&quot;&gt;4&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. &quot;The ulp at x&quot; means the step between neighboring
points where x lives. E4M3&#x27;s ulp at 1.0 is 0.125; its ulp at 300
is 32. One number, the ulp at 1.0, summarizes a format&#x27;s whole
precision, because every other interval&#x27;s step is that number times a
power of two; the field guide later on this page prints it on
every format&#x27;s card.&lt;&#x2F;p&gt;
&lt;p&gt;The proof under the map is 15 lines of Python that decode all
256 bit patterns from first principles, and torch agrees with
every single one. The largest value is 448. The smallest
positive value is 0.001953125. Divide them: 448 &#x2F; 0.001953125 =
229,376, about 2^17.8, so the top of this format is about 18
doublings above the bottom. Now compare that with the second
drawing: one tensor&#x27;s gradients spanned 20.9 doublings. The
whole format is narrower than one tensor&#x27;s spread, and that gap
runs the entire training half of this page.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-edges-of-the-map&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-edges-of-the-map&quot; aria-label=&quot;Anchor link for: the-edges-of-the-map&quot;&gt;The edges of the map&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Four places on the map need special handling, and every format
must decide all four.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Zero.&lt;&#x2F;strong&gt; The position field always means &quot;1.something&quot;, so no
pattern naturally means zero. The all-zeros pattern is simply
assigned the value zero, as a special case. The sign bit still exists, so there is
a +0 and a -0, and they compare equal.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;The ramp.&lt;&#x2F;strong&gt; Just above zero there is trouble. Every normal
point is 1.something x a power of 2. The exponent has a lowest
power, so there is a smallest normal point: in E4M3 it is
1.0 x 2^-6 = 0.015625. Below it, a naive format has nothing
until 0. A cliff. The fix: in the lowest interval only, drop
the hidden 1. Read the mantissa as 0.something instead of
1.something. Then 0.000 x 2^-6 is 0 itself, and the other 7
patterns walk up from 0 in the same equal steps. These points
are the &lt;strong&gt;subnormals&lt;&#x2F;strong&gt;. They turn the cliff into a ramp:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 974px;&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;chfp&#x2F;drawings&#x2F;subnormal-ramp-light.svg?h=45d73f2855618843c258&quot;
         alt=&quot;2 number lines near zero: on the first, a dashed empty box between 0 and the smallest normal reads no points anywhere in here, and a small result&amp;#x27;s arrow falls through it to 0; on the second, 7 equal orange steps of 0.00195 fill the gap and the same result&amp;#x27;s arrow lands on a step&quot; width=&quot;974&quot; height=&quot;337&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;chfp&#x2F;drawings&#x2F;subnormal-ramp-dark.svg?h=b79248472ab6f8d7c4ea&quot;
         alt=&quot;2 number lines near zero: on the first, a dashed empty box between 0 and the smallest normal reads no points anywhere in here, and a small result&amp;#x27;s arrow falls through it to 0; on the second, 7 equal orange steps of 0.00195 fill the gap and the same result&amp;#x27;s arrow lands on a step&quot; width=&quot;974&quot; height=&quot;337&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 gap at zero, empty and then filled. gradual underflow means the difference of two unequal numbers always has somewhere to land.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;&lt;strong&gt;Infinity.&lt;&#x2F;strong&gt; When a result overflows the ceiling, float32 returns
a dedicated pattern called &lt;code&gt;inf&lt;&#x2F;code&gt;, which then behaves lawfully:
anything finite divided by &lt;code&gt;inf&lt;&#x2F;code&gt; is 0, and &lt;code&gt;inf - inf&lt;&#x2F;code&gt; is the next
special value. E4M3 made a harder choice: it has no infinity at
all. Those patterns were used for one more doubling of range,
and overflow stops at 448 instead. Keep that choice in mind; it
is the first sign of how little room 8 bits leave.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;NaN.&lt;&#x2F;strong&gt; Not a Number is the pattern returned when no answer is
defensible: 0&#x2F;0, inf - inf, the square root of -1. It has one
deliberately strange law: &lt;code&gt;nan == nan&lt;&#x2F;code&gt; is false, so &lt;code&gt;x != x&lt;&#x2F;code&gt; is
the honest test for it. When your loss prints &lt;code&gt;nan&lt;&#x2F;code&gt;, this value
is what you are looking at, and it arrived through an overflow or
an undefined step somewhere upstream. We will catch it in the act
in a few sections.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;rounding&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#rounding&quot; aria-label=&quot;Anchor link for: rounding&quot;&gt;Rounding&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Between any 2 neighboring points of the map lies everything the format cannot
say. When a result lands there, one rule decides its fate: go to
the nearer point. When it lands exactly halfway, go to the point
whose last mantissa bit is 0, so that ties break upward and
downward equally often and a long chain of them does not drift.
This pair of rules is round-to-nearest, ties-to-even, the default
of every machine you will touch &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 686px;&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;chfp&#x2F;drawings&#x2F;rounding-line-light.svg?h=52bcc68b3906c9efecda&quot;
         alt=&quot;3 bands: a number between 1.250 and 1.375 with distance brackets 0.7 of the step and 0.3, and an arrow to the nearer point; the tie drawn: 1.1875 exactly between 1.125, mantissa ending 001, odd, and 1.250, ending 010, even, wins, with equal-distance brackets and the arrow to 1.250; and 1.2875 under the 4 rounding modes as 4 aligned arrow rows, nearest, toward 0 and toward minus inf to 1.250, toward plus inf to 1.375&quot; width=&quot;686&quot; height=&quot;537&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;chfp&#x2F;drawings&#x2F;rounding-line-dark.svg?h=40118d80c4d09359620e&quot;
         alt=&quot;3 bands: a number between 1.250 and 1.375 with distance brackets 0.7 of the step and 0.3, and an arrow to the nearer point; the tie drawn: 1.1875 exactly between 1.125, mantissa ending 001, odd, and 1.250, ending 010, even, wins, with equal-distance brackets and the arrow to 1.250; and 1.2875 under the 4 rounding modes as 4 aligned arrow rows, nearest, toward 0 and toward minus inf to 1.250, toward plus inf to 1.375&quot; width=&quot;686&quot; height=&quot;537&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; between two points, the nearer one wins; a tie goes to the even one. the whole error model of numerical computing is this picture.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The worst this rule can do is move a value by half the local
step: half an ulp. For float32 the ulp at 1.0 is 2 to the power -23, which is
about 1.2e-7 (read e-7 as: move the decimal point 7 places left,
so 0.00000012), and that number is called the machine epsilon
(&lt;code&gt;torch.finfo(torch.float32).eps&lt;&#x2F;code&gt; prints it). So every operation lands
within about 6e-8 of the true result, measured relative to the
result&#x27;s size. Both numbers read straight
off the map: find the interval that starts at 1, take its step, halve
it. The standard error analysis of floating point is this one
bound, applied once per operation &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-4&quot;&gt;4&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;How can hardware apply the rule without computing every bit of
the exact answer first? It keeps 3 extra bits, and that is all
it ever needs:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 770px;&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;chfp&#x2F;drawings&#x2F;three-extra-bits-light.svg?h=97345a80a41e7d839c19&quot;
         alt=&quot;the exact product of 2 24-bit numbers drawn as a 48-bit bar cut after the 24 bits the format keeps; the first 2 lost bits drop into orange intervals g and r; dashed lines funnel every later bit into s, 1 if any later bit was 1; guard, round, sticky alone answer every nearest-or-tie question, exactly&quot; width=&quot;770&quot; height=&quot;238&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;chfp&#x2F;drawings&#x2F;three-extra-bits-dark.svg?h=ae46c67c5eb017d6fe8b&quot;
         alt=&quot;the exact product of 2 24-bit numbers drawn as a 48-bit bar cut after the 24 bits the format keeps; the first 2 lost bits drop into orange intervals g and r; dashed lines funnel every later bit into s, 1 if any later bit was 1; guard, round, sticky alone answer every nearest-or-tie question, exactly&quot; width=&quot;770&quot; height=&quot;238&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; guard, round, sticky: the whole rounding rule runs on 3 extra bits, and the result is exactly as if the machine had kept all 48.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;This explains the first code block at the top of the page, and
you can check every step of it by hand.
To write 0.1 in binary you double it, again and again, and each
time the whole part of the result is the next bit. 0.1 doubles to
0.2: the first bit is 0. Then 0.4, 0.8: two more 0s. Then 0.8
doubles to 1.6: the first 1, and the 0.6 carries on. 0.6 doubles
to 1.2: another 1, and 0.2 carries on. But 0.2 is where the
second step started. The process is in a loop, and the bits
repeat forever:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 849px;&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;chfp&#x2F;drawings&#x2F;point-one-tail-light.svg?h=8d9b7171796e814a11b4&quot;
         alt=&quot;the doubling ladder that converts 0.1 to binary: 5 rows each double the fraction and record the whole part as the next bit, giving 0 0 0 1 1; the leftover 0.2 loops back to the second row along an orange arrow; below, the bit string with 3 consecutive 0011 blocks bracketed, the same block forever, and an orange cut mark where the format cuts, bit 53 for float64, and rounds, storing exactly 0.1000000000000000055511151231257827&quot; width=&quot;849&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;chfp&#x2F;drawings&#x2F;point-one-tail-dark.svg?h=28032e9a3d5bb6182e3b&quot;
         alt=&quot;the doubling ladder that converts 0.1 to binary: 5 rows each double the fraction and record the whole part as the next bit, giving 0 0 0 1 1; the leftover 0.2 loops back to the second row along an orange arrow; below, the bit string with 3 consecutive 0011 blocks bracketed, the same block forever, and an orange cut mark where the format cuts, bit 53 for float64, and rounds, storing exactly 0.1000000000000000055511151231257827&quot; width=&quot;849&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 17.&lt;&#x2F;span&gt; 0.1 converted by hand. 5 doublings and the state returns to 0.2: the tail 0011 repeats forever, and no finite mantissa can hold it.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;1&#x2F;3 does the same thing in decimal, and for the same reason: the
denominator has a prime factor the base does not. Ten is 2 times
5, so 0.1 is 1&#x2F;(2 times 5), and base two has no 5. The format
cuts the repetition at its mantissa width and stores the nearest
representable number, which for float64 is exactly
0.1000000000000000055511151231257827... (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p0-riddles&quot;&gt;proof&lt;&#x2F;a&gt;
prints every digit). The same happens to 0.2 and to 0.3, and the
sum of the two stored numbers is not the stored number nearest
0.3.
Nothing malfunctioned. 3 numbers you typed do not exist,
and the printed digits show exactly which nearby numbers were
stored instead.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p0-riddles&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_riddles.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 two riddles. 0.1 + 0.2 is not 0.3 because 0.1 and 0.2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;do not exist in binary; the sum of the two nearest citizens is not&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 nearest citizen to 0.3. And at 1e8, float32&amp;#39;s local step is 8,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;so adding 1 moves nothing.&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;from&lt;&#x2F;span&gt;&lt;span&gt; fractions&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Fraction&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; struct&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;&#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 exact value a float64 stores for 0.1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;exact&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Fraction&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;struct&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;unpack&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;&amp;lt;q&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; struct&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;pack&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;&amp;lt;d&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.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;0&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; &amp;amp;&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-keyword z-operator&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;52&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-keyword z-operator&quot;&gt; |&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-keyword z-operator&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;52&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-punctuation z-definition z-comment z-comment&quot;&gt;# simpler and fully honest: print the stored values to 30 digits&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;stored 0.1  = &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;0.1&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.30f&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;stored 0.2  = &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;0.2&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.30f&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;stored sum  = &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;0.1&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; 0.2&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.30f&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;stored 0.3  = &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;0.3&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.30f&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;0.1 + 0.2 == 0.3 : &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;0.1&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; 0.2&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; 0.3&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;&#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;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 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;float32: 1e8 + 1 - 1e8 = &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&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;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;the local step at 1e8  = &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&gt;nextafter&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; 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;2e8&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;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;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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p0_riddles.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;One more piece of standard equipment: the fused multiply-add, 1
instruction computing a times b plus c with a single rounding at
the end instead of 2. The result is as if the product had been
computed exactly and only the final sum were rounded, and on
modern chips it costs about as much as 1 multiply &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-27&quot;&gt;27&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The
difference is not small print. Choose a = b = 1 + 2^-27 and
c = -(1 + 2^-26): the separate path rounds the product first and
returns exactly 0, while the fused path returns the true answer,
2^-54 (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p21-fma-one-rounding&quot;&gt;proof&lt;&#x2F;a&gt; runs both on this
machine). Every tensor core in the training half of this page is
a lattice of these fused units, and the sums they accumulate
stay in float32 even when the products arrive in 8 bits. Keep
that sentence; it is why low-precision training is possible at
all.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p21-fma-one-rounding&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;p21_fma_one_rounding.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 fused multiply-add really is one rounding instead of&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;2, and the difference is visible. Choose a, b and c so that the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;exact product a*b carries a tail the separate path must round away:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  a = b = 1 + 2^-27, so a*b = 1 + 2^-26 + 2^-54 exactly&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  c = -(1 + 2^-26)&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 separate path rounds a*b to 1 + 2^-26 (the 2^-54 tail is under&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;half an ulp), then adds c and returns exactly 0.0: it reports that&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;a*b + c is zero when it is not. The fused path computes the exact&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;product, adds c, rounds once, and returns the true answer, 2^-54.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Both answers are legal for the source line &amp;quot;a*b + c&amp;quot;, which is why&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;a compiler&amp;#39;s fusion choice can change a program&amp;#39;s bits.&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; math&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;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&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 class=&quot;z-constant z-numeric&quot;&gt; 1.0&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.0&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;27&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 class=&quot;z-keyword z-operator&quot;&gt; -&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.0&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.0&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;26&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;separate&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;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; c&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 class=&quot;z-support&quot;&gt; hasattr&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;math&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;fma&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;    fused&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fma&lt;&#x2F;span&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-punctuation&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;span&gt; c&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;          # Python 3.13+&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    how&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-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;math.fma&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 class=&quot;z-keyword&quot;&gt;else&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;    import&lt;&#x2F;span&gt;&lt;span&gt; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;util&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;CDLL&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;util&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;find_library&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;m&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;    libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fma&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;restype&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;c_double&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fma&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;argtypes&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;ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;c_double&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; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fused&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fma&lt;&#x2F;span&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-punctuation&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;span&gt; c&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;          # the C library&amp;#39;s fma&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    how&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-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;libm fma&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;&#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;a = b = 1 + 2^-27, c = -(1 + 2^-26)&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;separate (a*b) + c : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;separate&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;fused fma(a, b, c) : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;fused&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   (via &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;how&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;true answer        : &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;2.0&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;54&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-keyword&quot;&gt;assert&lt;&#x2F;span&gt;&lt;span&gt; separate&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; 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;assert&lt;&#x2F;span&gt;&lt;span&gt; fused&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.0&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;54&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;t&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;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&gt;a&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;float64&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;&#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;tensor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&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;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;float64&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;&#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;tensor&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-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;float64&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-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 eager a*b+c  : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   (separate ops: also 0.0)&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-constant&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;the separate path says the answer is exactly 0 when it is&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;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;not; the fused path keeps it. both are legal readings of the&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;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 source line, which is why fusion decisions (a compiler&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;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;flag, an inlined function, torch.compile) can change a&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;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;program&amp;#39;s last bits without anyone touching the math.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p21_fma_one_rounding.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;The unit itself is worth one look, because the training half of
this page keeps returning to it:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 776px;&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;chfp&#x2F;drawings&#x2F;tensor-core-fma-light.svg?h=cc75ec3dd0ba551973d9&quot;
         alt=&quot;one fused multiply-add unit drawn as a flow: two 8-bit inputs enter a multiply, the exact product with every bit kept enters an add, and the result drops into a float32 accumulator with the note 1 rounding, here only; the accumulator loops back into the add; at the right, a faint 3 by 3 lattice of dashed units, a tensor core is a lattice of these units, side by side&quot; width=&quot;776&quot; height=&quot;211&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;chfp&#x2F;drawings&#x2F;tensor-core-fma-dark.svg?h=dc3b03b480fb23bce6a5&quot;
         alt=&quot;one fused multiply-add unit drawn as a flow: two 8-bit inputs enter a multiply, the exact product with every bit kept enters an add, and the result drops into a float32 accumulator with the note 1 rounding, here only; the accumulator loops back into the add; at the right, a faint 3 by 3 lattice of dashed units, a tensor core is a lattice of these units, side by side&quot; width=&quot;776&quot; height=&quot;211&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; one fused multiply-add unit. the product keeps every bit, the running sum lives in float32, and the single rounding happens at the end. a tensor core is a lattice of these.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;h2 id=&quot;where-arithmetic-goes-wrong&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#where-arithmetic-goes-wrong&quot; aria-label=&quot;Anchor link for: where-arithmetic-goes-wrong&quot;&gt;Where arithmetic goes wrong&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Three rules of ordinary arithmetic stop holding once every
result is rounded to a point, and all three cause real bugs.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Absorption.&lt;&#x2F;strong&gt; At 100,000,000, which is 1e8, float32&#x27;s step is 8, so
adding 1 offers a move smaller than half the gap, and the sum
rounds straight back (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p4-absorb-cancel&quot;&gt;proof&lt;&#x2F;a&gt;):&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 660px;&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;chfp&#x2F;drawings&#x2F;absorption-step-light.svg?h=935ff018283bab0f9474&quot;
         alt=&quot;one interval of the float32 map framed between 100000000 and 100000008; a thick orange plus 1 arrow reaches an eighth of the way across, short of the dashed halfway tick at plus 4, and a dashed return path brings the sum back to the left point, short of halfway, the sum rounds back; below, times 1,000,000, the same landing every time&quot; width=&quot;660&quot; height=&quot;221&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;chfp&#x2F;drawings&#x2F;absorption-step-dark.svg?h=852b4e406197f726ba5b&quot;
         alt=&quot;one interval of the float32 map framed between 100000000 and 100000008; a thick orange plus 1 arrow reaches an eighth of the way across, short of the dashed halfway tick at plus 4, and a dashed return path brings the sum back to the left point, short of halfway, the sum rounds back; below, times 1,000,000, the same landing every time&quot; width=&quot;660&quot; height=&quot;221&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; 1e8 + 1 = 1e8, exactly. the addend was real; the local step is 8; there is nowhere closer to land.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p4-absorb-cancel&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_absorb_cancel.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 two classic accidents. Absorption: below half the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;local step, an addend vanishes. Cancellation: subtracting two close&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;numbers deletes their shared leading digits and promotes the noise.&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;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;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 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;float32 step at 1e8      : &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;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;nextafter&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; 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;2&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;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;1e8 + 1                  : &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&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&gt;item&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;:.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;   (absorbed)&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;1e8 + 5                  : &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&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; 5&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;span class=&quot;z-storage z-type&quot;&gt;:.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;   (rounds up: past half the step)&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;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;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;1.0000001&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;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;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;1.0000000&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;d&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;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;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-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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;(1.0000001 - 1.0) in float32 = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;d&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.10e&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;true answer                  = 1.0000000e-07&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;relative error               = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;d&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; 1e-7&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;1e-7&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.1%&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-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;seven leading digits matched and left; only the rounding&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;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;noise of the inputs remained.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p4_absorb_cancel.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;&lt;strong&gt;Cancellation.&lt;&#x2F;strong&gt; Subtract two nearly equal numbers and their
shared leading digits leave together, promoting whatever rounding
noise the inputs carried into the leading digits of the answer.
The proof above subtracts 1.0000001 from 1.0 in float32 and gets
1.19e-7 where the truth is 1.0e-7: a 19% error from one
subtraction of two almost-exact inputs. The strange part: the
subtraction itself commits no error at all. When 2 numbers are
within a factor of 2 of each other, their difference is always
exactly representable (Sterbenz&#x27;s lemma &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-4&quot;&gt;4&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p22-exact-error&quot;&gt;proof&lt;&#x2F;a&gt; checks it on this very case).
Cancellation never creates error; it exposes the error the
inputs already carried.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Order.&lt;&#x2F;strong&gt; Because every addition rounds, (a + b) + c and
a + (b + c) are different numbers. Sum 50,000 values
forward and backward and the answers disagree
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p5-sum-order&quot;&gt;proof&lt;&#x2F;a&gt;); a parallel machine that splits the
same sum across cores picks yet another order, and another answer.
torch&#x27;s own &lt;code&gt;.sum()&lt;&#x2F;code&gt; adds pairwise in a tree, which is both
faster and about 130 times closer to the true sum than a
one-by-one loop (measured: 0.24 error against 0.0018), and compensated summation (Kahan&#x27;s trick of carrying
the rounding error in a second variable) closes most of the rest
&lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-4&quot;&gt;4&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p5-sum-order&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;p5_sum_order.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: addition order changes the answer. One million float32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;values, three orders, three sums; then the repairs: pairwise&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;(what torch does), Kahan compensation, and fsum as ground truth.&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; math&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;&#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;x&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;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;1_000_000&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; 100&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&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;truth&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fsum&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;double&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;tolist&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;fwd&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;0.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 class=&quot;z-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; c&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&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;split&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;100_000&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;          # sequential in chunks, forward&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; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&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-keyword&quot;&gt; pass&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;# a plain python loop over 1e6 floats is slow; do exact fp32 fold in torch&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; fold&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;t&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;    s&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;0.0&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 class=&quot;z-keyword&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&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;split&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;4096&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; u&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&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;tolist&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;            s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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&gt;u&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 class=&quot;z-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; s&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&gt;seq_fwd&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; fold&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 class=&quot;z-constant z-numeric&quot;&gt;50_000&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;          # smaller slice keeps runtime sane&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;seq_rev&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; fold&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 class=&quot;z-constant z-numeric&quot;&gt;50_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;flip&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;tsum&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 class=&quot;z-constant z-numeric&quot;&gt;50_000&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;span&gt;item&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;   # torch&amp;#39;s pairwise-style reduction&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;truth50&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fsum&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 class=&quot;z-constant z-numeric&quot;&gt;50_000&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;double&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;tolist&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;ground truth (fsum)       : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;truth50&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.6f&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;sequential, forward       : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;seq_fwd&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.6f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   err &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;seq_fwd&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;truth50&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:+.6f&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;sequential, reversed      : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;seq_rev&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.6f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   err &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;seq_rev&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;truth50&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:+.6f&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 .sum() (pairwise)   : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;tsum&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.6f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   err &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;tsum&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;truth50&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:+.6f&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;forward == reversed       : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;seq_fwd&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; seq_rev&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p5_sum_order.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;One more fact turns this whole section from a list of failures
into a toolbox: the rounding error is catchable, exactly. The
error of an addition is itself a number the format can hold, and
3 operations recover it whole: s = a + b, z = s - a, e = b - z,
and now s + e equals the true sum of a and b with no error at
all. Run it on this page&#x27;s opening example and the 3 operations
hand back the exact rounding error of 0.1 + 0.2
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p22-exact-error&quot;&gt;proof&lt;&#x2F;a&gt; checks it against exact rational
arithmetic). A fused multiply-add does the same for a product in
1 instruction. Kahan&#x27;s compensated sum is this trick run in a
loop, and the double-word arithmetic that stretches precision in
software is this trick kept instead of thrown away &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-4&quot;&gt;4&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p22-exact-error&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;p22_exact_error.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 rounding error is catchable, exactly. 3 facts, each&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;checked against exact rational arithmetic (fractions.Fraction):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;1. Fast2Sum: for |a| &amp;gt;= |b|, 3 operations recover the rounding&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;   error of a + b perfectly: s = a+b; z = s-a; e = b-z. Then&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;   s + e equals the true sum of the stored a and b, exactly.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;   Run on the page&amp;#39;s opening example, 0.1 + 0.2.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;2. Sterbenz: when x and y are within a factor of 2 of each&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;   other, x - y is computed exactly: cancellation commits no&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;   error of its own; it only exposes error the inputs carried.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;3. 2MultFMA: 1 fused multiply-add recovers a product&amp;#39;s whole&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;   rounding error: e = fma(a, b, -a*b).&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;from&lt;&#x2F;span&gt;&lt;span&gt; fractions&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Fraction&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; F&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; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;util&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. Fast2Sum on 0.1 + 0.2&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-punctuation&quot;&gt;,&lt;&#x2F;span&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 class=&quot;z-constant z-numeric&quot;&gt; 0.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; 0.1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;s&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;z&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; z&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;s = a + b        = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;s&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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;e = (b - (s - a)) = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;e&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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-keyword&quot;&gt;assert&lt;&#x2F;span&gt;&lt;span&gt; F&lt;&#x2F;span&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-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; F&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&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;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; F&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;s&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; F&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;&#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;checked: stored 0.2 + stored 0.1 == s + e, EXACTLY.&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;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;the 3 float operations caught the rounding error whole.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;&#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. Sterbenz: close subtraction is exact&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-punctuation&quot;&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 class=&quot;z-constant z-numeric&quot;&gt; 1.0000001&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.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;d&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&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; F&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 class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; F&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 class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; F&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;d&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 - y = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;d&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;checked: equals the exact difference of the stored inputs.&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;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;the 19&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;% e&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;rror of the cancellation section was already in&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;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;the inputs; the subtraction added nothing.&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;&#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. 2MultFMA: a product&amp;#39;s error in one instruction&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;CDLL&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;util&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;find_library&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;m&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;libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fma&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;restype&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;c_double&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fma&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;argtypes&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;ctypes&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;c_double&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; 3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;u&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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 class=&quot;z-constant z-numeric&quot;&gt; 1.0&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.0&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;27&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; u&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;e2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; libm&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;fma&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;u&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 class=&quot;z-keyword z-operator&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-keyword&quot;&gt;assert&lt;&#x2F;span&gt;&lt;span&gt; F&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;u&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; F&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 class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; F&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; +&lt;&#x2F;span&gt;&lt;span&gt; F&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;e2&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;p = u * v       = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;p&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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;e = fma(u,v,-p) = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;e2&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;checked: stored u times stored v == p + e, exactly.&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;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;Kahan&amp;#39;s compensated sum is fact 1 run in a loop; the&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;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;double-word arithmetic that stretches precision in software&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;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;is facts 1 and 3 kept instead of thrown away.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p22_exact_error.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;Keep the order fact in mind. It returns at the end of this page as
the reason two identical training runs on two GPUs never match to
the last bit.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-field-guide&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-field-guide&quot; aria-label=&quot;Anchor link for: the-field-guide&quot;&gt;The field guide&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The formats in this guide carry nearly all of the world&#x27;s
floating-point arithmetic, and you now own every idea needed to
read them. Each gets the same treatment: its card, the bit
counts and the numbers that follow from them; its map, where
its points land; where it came from; where it runs today; and
how it fails. Every number on every card is derived from the 2
bit counts alone, with the formulas you used on binary32, and
checked against torch&#x27;s own tables
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p16-wide-formats&quot;&gt;proof&lt;&#x2F;a&gt; runs the check). The guide
covers the 4 wide formats, the ones that stand alone. The 8-,
6- and 4-bit formats enter later, each at the exact place in
the training story that needs it, and a closing sheet gathers
the whole family on one drawing.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p16-wide-formats&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;p16_wide_formats.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 field guide&amp;#39;s numbers, derived by hand from each&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;format&amp;#39;s two counts (E exponent bits, M mantissa bits), then checked&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;against torch. Nothing on the cards is typed from memory:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  bias        = 2^(E-1) - 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  ceiling     = (2 - 2^-M) * 2^bias&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  floor       = 2^(1 - bias)          (smallest normal)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  ramp floor  = 2^(1 - bias - M)      (smallest subnormal)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  step at 1   = 2^-M                  (the ulp of 1.0; finfo calls it eps)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  cells       = 2^E - 2               (power-of-two intervals)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  points&#x2F;cell = 2^M&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Also prints the torch defaults the float32 section quotes.&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; math&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-constant&quot;&gt;FORMATS&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-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;float64&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; 11&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; 52&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;float64&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;int64&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;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 class=&quot;z-constant z-numeric&quot;&gt; 8&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; 23&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;float32&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;int32&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;float16&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; 5&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;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float16&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;int16&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;bfloat16&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; 8&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; 7&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;bfloat16&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;int16&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;&#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;for&lt;&#x2F;span&gt;&lt;span&gt; name&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; M&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; it&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; FORMATS&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;    bias&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; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&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-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 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;span&gt;    ceiling&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment 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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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 z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    floor&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; bias&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;    ramp_floor&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    step_at_1&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ** -&lt;&#x2F;span&gt;&lt;span&gt;M&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cells&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; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; E&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&gt;    digits&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;M&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; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log10&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;    fi&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;finfo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;dt&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;    # smallest subnormal: bit pattern 0...01 viewed as this dtype&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    sub&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;1&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;it&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&gt;dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;double&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-keyword&quot;&gt;    assert&lt;&#x2F;span&gt;&lt;span&gt; ceiling&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;, (&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; ceiling&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&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;    assert&lt;&#x2F;span&gt;&lt;span&gt; floor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tiny&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;, (&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; floor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tiny&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;    assert&lt;&#x2F;span&gt;&lt;span&gt; step_at_1&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;, (&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; step_at_1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eps&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;    assert&lt;&#x2F;span&gt;&lt;span&gt; ramp_floor&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; sub&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;, (&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; ramp_floor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; sub&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;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:9s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; E=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;E&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:2d&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; M=&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-storage z-type&quot;&gt;:2d&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;  bias &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;bias&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:4d&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;ceiling &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;ceiling&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4g&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;          floor &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;floor&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4g&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   ramp floor &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;ramp_floor&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4g&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;step at 1 = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;step_at_1&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4g&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;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;          {&lt;&#x2F;span&gt;&lt;span&gt;cells&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; cells of &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;2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;M&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&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; points; &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;about &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;digits&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.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; decimal digits   &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;(torch.finfo agrees on all four numbers)&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;# absorption never leaves; it only moves. float64&amp;#39;s step at 1e16 is 2.&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;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;1e16&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;float64&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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;float64: (1e16 + 1) - 1e16 = &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;x&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; x&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;span class=&quot;z-storage z-type&quot;&gt;:g&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;   (the step at 1e16 is 2; the 1 is below half 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;&#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 defaults the float32 section quotes, read from this install&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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;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; defaults:&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;  default dtype                  &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&gt;get_default_dtype&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;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;  float32_matmul_precision       &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;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;get_float32_matmul_precision&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;!r&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;  backends.cuda.matmul.allow_tf32  &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;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;cuda&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;matmul&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;allow_tf32&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;  backends.cudnn.allow_tf32        &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;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;cudnn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;allow_tf32&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;  autocast dtype on cpu          &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;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;get_autocast_dtype&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;cpu&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-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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p16_wide_formats.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;float64&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#float64&quot; aria-label=&quot;Anchor link for: float64&quot;&gt;float64&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Double binary32&#x27;s byte count and you get the format the 1985
standard called double precision:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 766px;&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;chfp&#x2F;drawings&#x2F;float64-card-light.svg?h=24d878600415d3e76364&quot;
         alt=&quot;the bit fields of float64 drawn to scale: 1 sign bit, 11 exponent bits in orange, 52 mantissa bits, 8 bytes per number; a card lists bias 1023, ceiling 1.8e308, normal floor 2.23e-308, ramp floor 4.94e-324, step at 1.0 of 2.22e-16, and about 16 decimal digits; brackets at the right tie bias, ceiling and floor to the exponent field, ramp floor to both fields, step and digits to the mantissa field&quot; width=&quot;766&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;float64-card-dark.svg?h=1fd26fc4d4fcc8a9675f&quot;
         alt=&quot;the bit fields of float64 drawn to scale: 1 sign bit, 11 exponent bits in orange, 52 mantissa bits, 8 bytes per number; a card lists bias 1023, ceiling 1.8e308, normal floor 2.23e-308, ramp floor 4.94e-324, step at 1.0 of 2.22e-16, and about 16 decimal digits; brackets at the right tie bias, ceiling and floor to the exponent field, ramp floor to both fields, step and digits to the mantissa field&quot; width=&quot;766&quot; height=&quot;342&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; float64&#x27;s card. 11 bits of interval, 52 of position; every row follows from those two counts (proof below the guide&#x27;s intro).&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The bias is 1023, so the intervals run from 2.2e-308 up to 1.8e308:
2,046 of them, each holding 4,503,599,627,370,496 points. The
ulp at 1.0 is 2.22e-16, which is about 16 decimal digits of
position. A map that wide can only be drawn with breaks:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 895px;&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;chfp&#x2F;drawings&#x2F;float64-map-light.svg?h=06caf186ebf884150510&quot;
         alt=&quot;the float64 map: a dashed ramp interval, then power of two intervals drawn in 3 groups separated by break marks, 2,046 intervals each holding the same 4,503,599,627,370,496 points, running from 2.23e-308 to 1.8e308 with inf past the ceiling and a step chip hanging from a top interval; below, the interval from 1 to 2 magnified showing its first 16 steps, with a bracket at the first step: 2 to the -52 equals 2.22e-16, the ulp at 1.0&quot; width=&quot;895&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;chfp&#x2F;drawings&#x2F;float64-map-dark.svg?h=e0ee646d884706f680c6&quot;
         alt=&quot;the float64 map: a dashed ramp interval, then power of two intervals drawn in 3 groups separated by break marks, 2,046 intervals each holding the same 4,503,599,627,370,496 points, running from 2.23e-308 to 1.8e308 with inf past the ceiling and a step chip hanging from a top interval; below, the interval from 1 to 2 magnified showing its first 16 steps, with a bracket at the first step: 2 to the -52 equals 2.22e-16, the ulp at 1.0&quot; width=&quot;895&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 21.&lt;&#x2F;span&gt; the float64 map: the E4M3 picture with 2,046 intervals instead of 15, and 2^52 points per interval instead of 2^3.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Where it came from: IEEE 754 began as the arithmetic William
Kahan designed with Intel for the 8087, the floating-point
coprocessor sold beside the 8086; the standards committee turned
that chip&#x27;s arithmetic into everyone&#x27;s law &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;][&lt;a href=&quot;#ref-18&quot;&gt;18&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. float64 was
its wide grade, sized so ordinary science could chain millions
of operations and still trust the leading digits.&lt;&#x2F;p&gt;
&lt;p&gt;Where it runs today: everywhere you did not choose a dtype. A
Python float is a float64. NumPy builds float64 arrays unless
told otherwise. The digits of 0.1 printed in the rounding
section were float64 digits.&lt;&#x2F;p&gt;
&lt;p&gt;Its proof: &lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p16-wide-formats&quot;&gt;p16&lt;&#x2F;a&gt; derives the whole card
from E and M and checks every row against &lt;code&gt;torch.finfo&lt;&#x2F;code&gt;. One
extra line in it repeats the absorption experiment at this
format&#x27;s scale: in float64, (1e16 + 1) - 1e16 = 0. The ulp at
1e16 is 2, and the added 1 fell under half of it. 16 digits
push the cliff out of sight; no digit count removes it.&lt;&#x2F;p&gt;
&lt;p&gt;How it fails in a training loop: float64 rarely gives training
a wrong answer. Its problem is cost. 8 bytes per number is 2
times float32 and 4 times bfloat16, spent on digits training
cannot use: the sections ahead show training running where the
step at 1.0 is 0.0078, so digits 4 through 16 add nothing. On
GPUs there is a second cost: most chips carry few float64 units,
and NVIDIA&#x27;s own throughput tables list float64 operations far
below float32 on most of its hardware &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-19&quot;&gt;19&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. torch&#x27;s default is
float32, and float64 appears only when you ask
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p16-wide-formats&quot;&gt;proof&lt;&#x2F;a&gt; prints the default).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;float32-and-the-format-hiding-inside-it&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#float32-and-the-format-hiding-inside-it&quot; aria-label=&quot;Anchor link for: float32-and-the-format-hiding-inside-it&quot;&gt;float32, and the format hiding inside it&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;binary32 you have already decoded by hand; the card collects its
numbers:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 766px;&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;chfp&#x2F;drawings&#x2F;float32-card-light.svg?h=1b1b55fa0d647c0f0d18&quot;
         alt=&quot;the bit fields of float32 drawn to scale: 1 sign bit, 8 exponent bits in orange, 23 mantissa bits, 4 bytes per number; a card lists bias 127, ceiling 3.4e38, normal floor 1.18e-38, ramp floor 1.4e-45, step at 1.0 of 1.19e-7 highlighted, and about 7 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;766&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;float32-card-dark.svg?h=ee93739f351a08908a17&quot;
         alt=&quot;the bit fields of float32 drawn to scale: 1 sign bit, 8 exponent bits in orange, 23 mantissa bits, 4 bytes per number; a card lists bias 127, ceiling 3.4e38, normal floor 1.18e-38, ramp floor 1.4e-45, step at 1.0 of 1.19e-7 highlighted, and about 7 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;766&quot; height=&quot;342&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; float32&#x27;s card. the orange row, 1.19e-7 at 1.0, is the machine epsilon from the rounding section.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;254 intervals from 1.2e-38 to 3.4e38, 8,388,608 points in each,
about 7 digits:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 806px;&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;chfp&#x2F;drawings&#x2F;float32-map-light.svg?h=9a4e1200dfa842fe83a1&quot;
         alt=&quot;the float32 map: a dashed ramp interval, then power of two intervals drawn in 3 groups separated by break marks, 254 intervals each holding the same 8,388,608 points, running from 1.18e-38 to 3.4e38 with inf past the ceiling and a step chip hanging from a top interval; below, the interval from 1 to 2 magnified showing its first 16 steps, with a bracket at the first step: 2 to the -23 equals 1.19e-7, the ulp at 1.0&quot; width=&quot;806&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;chfp&#x2F;drawings&#x2F;float32-map-dark.svg?h=b6877e7c6b499c252bca&quot;
         alt=&quot;the float32 map: a dashed ramp interval, then power of two intervals drawn in 3 groups separated by break marks, 254 intervals each holding the same 8,388,608 points, running from 1.18e-38 to 3.4e38 with inf past the ceiling and a step chip hanging from a top interval; below, the interval from 1 to 2 magnified showing its first 16 steps, with a bracket at the first step: 2 to the -23 equals 1.19e-7, the ulp at 1.0&quot; width=&quot;806&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 float32 map. the absorption step of 8 at 1e8 is one of these intervals, far to the right of 1.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Where it came from: the same 1985 standard&#x27;s single precision &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.
Where it runs today: it is the default dtype of torch;
&lt;code&gt;torch.tensor(1.0)&lt;&#x2F;code&gt; is a float32 unless you say otherwise
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p16-wide-formats&quot;&gt;proof&lt;&#x2F;a&gt; prints the default from this
install). Even when matmuls run narrow, the master weights of
mixed-precision training stay float32 and the tensor cores
accumulate in float32, so the training half of this page
keeps this format at its center.&lt;&#x2F;p&gt;
&lt;p&gt;Now the format hiding inside it: TensorFloat-32. Since the A100,
NVIDIA&#x27;s tensor cores can take an ordinary float32 matmul, round
each factor to 10 mantissa bits, multiply, and accumulate in
full float32 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-20&quot;&gt;20&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The tensors going in and out are ordinary
float32; only the multiply is narrow, reading 19 of the 32 bits
(1 + 8 + 10). TF32 is that mode, not a storage type you can give
a tensor. NVIDIA measured large speedups at matching accuracy on
deep-learning workloads &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-20&quot;&gt;20&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; for code that needs all seven
digits it is a silent cut, and that is why torch ships with
matmul TF32 off since version 1.12 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-21&quot;&gt;21&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. On this install,
&lt;code&gt;torch.backends.cuda.matmul.allow_tf32&lt;&#x2F;code&gt; is False, the cudnn
convolution flag is True, and
&lt;code&gt;torch.set_float32_matmul_precision(&quot;high&quot;)&lt;&#x2F;code&gt; is the one-line
opt-in (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p16-wide-formats&quot;&gt;proof&lt;&#x2F;a&gt; prints all three). None
of this can execute on the Apple machine this page is measured
on; the flags are read here, the behavior is cited &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-20&quot;&gt;20&lt;&#x2F;a&gt;][&lt;a href=&quot;#ref-21&quot;&gt;21&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 871px;&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;chfp&#x2F;drawings&#x2F;tf32-card-light.svg?h=8a8c1c17451b6aacc702&quot;
         alt=&quot;the bit fields of TF32 drawn to scale: 1 sign bit, 8 exponent bits in orange, 10 mantissa bits, noted as 19 bits inside the tensor core; a card lists bias 127, ceiling 3.4e38, normal floor 1.18e-38, ramp floor 1.15e-41, step at 1.0 of 0.000977 highlighted, and about 3 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;871&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;tf32-card-dark.svg?h=443b7518cf8d857e3437&quot;
         alt=&quot;the bit fields of TF32 drawn to scale: 1 sign bit, 8 exponent bits in orange, 10 mantissa bits, noted as 19 bits inside the tensor core; a card lists bias 127, ceiling 3.4e38, normal floor 1.18e-38, ramp floor 1.15e-41, step at 1.0 of 0.000977 highlighted, and about 3 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;871&quot; height=&quot;342&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; TF32&#x27;s card: float32&#x27;s reach, float16&#x27;s digits, 19 bits, no storage form. it exists only in the middle of a tensor-core multiply.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;How it fails: you have watched it fail all page. The step of 8
at 1e8 is float32&#x27;s. The 7 digits that turned a subtraction of
near-equal inputs into a 19% error are float32&#x27;s. In
training, its failure is price: 4 bytes per number on the
memory bus that a later section shows is the bottleneck.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;float16&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#float16&quot; aria-label=&quot;Anchor link for: float16&quot;&gt;float16&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Cut 32 bits in half and something must go. There are two ways to
choose, and the choice split the 16-bit world in two:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 791px;&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;chfp&#x2F;drawings&#x2F;sixteen-fork-light.svg?h=9eca79d9c5b6c336dc8d&quot;
         alt=&quot;a fork: float32&amp;#x27;s 32-bit bar on top, 2 arrows labeled keep the digits and keep the reach diverging to float16, 5 exponent and 10 mantissa bits, and bfloat16, float32&amp;#x27;s top 16 bits; dashed guides tie bfloat16&amp;#x27;s exponent field to float32&amp;#x27;s; below, one shared log axis from 1e-38 to 1e38 carries each choice&amp;#x27;s reach: float16 a short bar from 6.1e-5 to 65504 with 3 digits, bfloat16 spanning float32&amp;#x27;s whole reach with 2 digits&quot; width=&quot;791&quot; height=&quot;363&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;chfp&#x2F;drawings&#x2F;sixteen-fork-dark.svg?h=5ec118a1aae9f48da995&quot;
         alt=&quot;a fork: float32&amp;#x27;s 32-bit bar on top, 2 arrows labeled keep the digits and keep the reach diverging to float16, 5 exponent and 10 mantissa bits, and bfloat16, float32&amp;#x27;s top 16 bits; dashed guides tie bfloat16&amp;#x27;s exponent field to float32&amp;#x27;s; below, one shared log axis from 1e-38 to 1e38 carries each choice&amp;#x27;s reach: float16 a short bar from 6.1e-5 to 65504 with 3 digits, bfloat16 spanning float32&amp;#x27;s whole reach with 2 digits&quot; width=&quot;791&quot; height=&quot;363&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; the fork. float16 kept digits and lost reach; bfloat16 kept float32&#x27;s whole exponent and pays in digits. training mostly chose reach.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;float16 is the left fork: keep digits, pay with reach.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 772px;&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;chfp&#x2F;drawings&#x2F;float16-card-light.svg?h=e33029e9888245f61a60&quot;
         alt=&quot;the bit fields of float16 drawn to scale: 1 sign bit, 5 exponent bits in orange, 10 mantissa bits, 2 bytes per number; a card lists bias 15, ceiling 65504 highlighted, normal floor 6.1e-5, ramp floor 5.96e-8, step at 1.0 of 0.000977, and about 3 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;772&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;float16-card-dark.svg?h=71d0599c9f14c1c38d58&quot;
         alt=&quot;the bit fields of float16 drawn to scale: 1 sign bit, 5 exponent bits in orange, 10 mantissa bits, 2 bytes per number; a card lists bias 15, ceiling 65504 highlighted, normal floor 6.1e-5, ramp floor 5.96e-8, step at 1.0 of 0.000977, and about 3 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;772&quot; height=&quot;342&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; float16&#x27;s card. the orange row is the ceiling, 65504: the number to remember about this format.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;5 exponent bits give bias 15 and only 30 intervals, from 6.1e-5 to
65504. 10 mantissa bits give 1,024 points per interval and about 3
digits. 30 intervals fit in one drawing, so this is the one wide
format whose map you can see whole:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 793px;&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;chfp&#x2F;drawings&#x2F;float16-map-light.svg?h=90ac06989f822b4519e8&quot;
         alt=&quot;the float16 map drawn whole: a dashed ramp interval and 30 power of two intervals on one unbroken line, each holding the same 1,024 points, from 6.1e-5 to 65504; a red arrow shoots past the ceiling: 60000 times 1.2 lands at inf; below, the interval from 1 to 2 magnified showing its first 16 steps, with a bracket at the first step: 2 to the -10 equals 0.000977, the ulp at 1.0&quot; width=&quot;793&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;chfp&#x2F;drawings&#x2F;float16-map-dark.svg?h=fba85dd656e31d5c202f&quot;
         alt=&quot;the float16 map drawn whole: a dashed ramp interval and 30 power of two intervals on one unbroken line, each holding the same 1,024 points, from 6.1e-5 to 65504; a red arrow shoots past the ceiling: 60000 times 1.2 lands at inf; below, the interval from 1 to 2 magnified showing its first 16 steps, with a bracket at the first step: 2 to the -10 equals 0.000977, the ulp at 1.0&quot; width=&quot;793&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 27.&lt;&#x2F;span&gt; all of float16, no breaks needed: 30 intervals end to end. compare the reach with the maps above it.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Where it came from: film and games, not a numerics committee.
NVIDIA and Microsoft made &lt;code&gt;half&lt;&#x2F;code&gt; a type in the Cg shading
language in 2002, and Industrial Light &amp;amp; Magic built its OpenEXR
film format on the same 1+5+10 layout, in production from 2000
and released as open source in 2003 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-22&quot;&gt;22&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. IEEE 754 adopted the
layout as binary16 in its 2008 revision &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The film industry
shipped the format years before the standard named it.&lt;&#x2F;p&gt;
&lt;p&gt;Where it runs today: graphics APIs and image pipelines still,
inference engines, and mixed-precision training on hardware from
before bfloat16 spread. In torch it is the half in &lt;code&gt;.half()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Its proof and its failure are the same three numbers. The
ceiling: 60000 times 1.2 is inf in float16
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p7-sixteen-bits&quot;&gt;proof&lt;&#x2F;a&gt;). The digits: 1.001 stores as
1.000977. The floor: normal numbers end at 6.1e-5, and the small
half of a gradient histogram lives below that, which is why the
loss-scaling section exists. When float16 dies in a run, it dies
at one of these three numbers.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p7-sixteen-bits&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;p7_sixteen_bits.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 16-bit fork, from finfo, nothing typed from memory.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;float16 spent its bits on precision and dies at 65504; bfloat16 kept&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;float32&amp;#39;s exponent and survives, with two decimal digits left.&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-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; dt&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&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;span&gt; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float16&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;bfloat16&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;    fi&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;finfo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;dt&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 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;dt&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;:16s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; max &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;12.4g&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   tiny &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tiny&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.3g&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;eps &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eps&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.3g&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&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;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;60000.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 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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;60000 * 1.2 in float16 : &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;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;half&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.2&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;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;60000 * 1.2 in bfloat16: &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;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;bfloat16&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; 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;1.2&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;bfloat16&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;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;1.001 in float16       : &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&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;1.001&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;float16&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;span class=&quot;z-storage z-type&quot;&gt;:.6f&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;1.001 in bfloat16      : &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&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;1.001&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;bfloat16&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;span class=&quot;z-storage z-type&quot;&gt;:.6f&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p7_sixteen_bits.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;bfloat16&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#bfloat16&quot; aria-label=&quot;Anchor link for: bfloat16&quot;&gt;bfloat16&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The right fork: keep reach, pay with digits. Google&#x27;s brain
float keeps all 8 of float32&#x27;s exponent bits and 7 of mantissa:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 766px;&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;chfp&#x2F;drawings&#x2F;bfloat16-card-light.svg?h=e8facc6b3d02597460be&quot;
         alt=&quot;the bit fields of bfloat16 drawn to scale: 1 sign bit, 8 exponent bits in orange, 7 mantissa bits, 2 bytes per number; a card lists bias 127, ceiling 3.39e38, normal floor 1.18e-38, ramp floor 9.18e-41, step at 1.0 of 0.00781 highlighted, and about 2 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;766&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;bfloat16-card-dark.svg?h=e2fd7b428c040a1821b4&quot;
         alt=&quot;the bit fields of bfloat16 drawn to scale: 1 sign bit, 8 exponent bits in orange, 7 mantissa bits, 2 bytes per number; a card lists bias 127, ceiling 3.39e38, normal floor 1.18e-38, ramp floor 9.18e-41, step at 1.0 of 0.00781 highlighted, and about 2 decimal digits; brackets tie each row to the field that sets it&quot; width=&quot;766&quot; height=&quot;342&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; bfloat16&#x27;s card: float32&#x27;s bias and reach, 7 bits of position. the orange row is the step that rounds weight updates away.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The reach is float32&#x27;s (the ceiling prints 3.39e38 rather than
3.4e38 only because the coarser last step lands the top point
lower), the intervals are the same 254 as float32&#x27;s, and each holds 128
points: about 2 decimal digits.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 761px;&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;chfp&#x2F;drawings&#x2F;bfloat16-map-light.svg?h=9a97cc4f83a8ad1e91ae&quot;
         alt=&quot;the bfloat16 map: a dashed ramp interval, then power of two intervals drawn in 3 groups separated by break marks, 254 intervals each holding the same 128 points, from 1.18e-38 to 3.39e38 with inf past the ceiling; below, the interval from 1 to 2 magnified with its step bracket, 2 to the -7 equals 0.00781, and under it a solid grey band: float32&amp;#x27;s 8,388,608 points in the same interval fuse into a band&quot; width=&quot;761&quot; height=&quot;373&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;chfp&#x2F;drawings&#x2F;bfloat16-map-dark.svg?h=0dd5be7b2aa5d1f7cea6&quot;
         alt=&quot;the bfloat16 map: a dashed ramp interval, then power of two intervals drawn in 3 groups separated by break marks, 254 intervals each holding the same 128 points, from 1.18e-38 to 3.39e38 with inf past the ceiling; below, the interval from 1 to 2 magnified with its step bracket, 2 to the -7 equals 0.00781, and under it a solid grey band: float32&amp;#x27;s 8,388,608 points in the same interval fuse into a band&quot; width=&quot;761&quot; height=&quot;373&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 bfloat16 map next to float32&#x27;s: the same intervals, 65,536 times fewer points in each. that one comparison is the entire format.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Where it came from: Google built it into the TPU&#x27;s matrix
units, which multiply in bfloat16 and accumulate in float32, and
chose the layout so that range would never be the problem and
conversion from float32 would be nearly free &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-23&quot;&gt;23&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The name is
Brain Floating Point, after the Google Brain team.&lt;&#x2F;p&gt;
&lt;p&gt;Nearly free is provable, and on this machine it is proven at the
bit level: bfloat16 is float32&#x27;s top half. Take any of the
65,536 possible bfloat16 bit patterns, cast it to float32, and
the result is the same 16 bits with 16 zeros appended;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p17-bf16-truncation&quot;&gt;proof&lt;&#x2F;a&gt; checks all 65,536, bit for
bit. The downward cast is the same move with rounding: keep the top
16 bits, round by the low 16. The proof checks
torch&#x27;s cast against that hand rule on 1,000,010 values (a
million random ones plus the edge cases), and they agree on
every one. Here is 1.7014 making the trip:&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;float32:  0 01111111 1011001 1100011101111010&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;bfloat16: 0 01111111 1011010   (the top half, rounded up by the rest)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;stored:   1.703125&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-p17-bf16-truncation&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;p17_bf16_truncation.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: bfloat16 is the top 16 bits of float32, at the bit level.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Two directions, both exhaustive where exhaustion is possible:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  up:   every one of the 65,536 bfloat16 bit patterns, cast to&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;        float32, must produce exactly (pattern &amp;lt;&amp;lt; 16): the same 16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;        bits on top, sixteen zeros appended below.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  down: casting float32 to bfloat16 must equal keeping the top 16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;        bits and rounding by the low 16 (nearest, ties to even),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;        checked on a million random values plus the edge cases.&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;&#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;# ---- up: bf16 -&amp;gt; fp32 is &amp;quot;append 16 zero bits&amp;quot;, all 65,536 patterns&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pats&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;65536&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;int32&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;to&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;int16&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;as_bf16&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pats&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;bfloat16&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;up_bits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; as_bf16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;int32&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;want&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pats&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;to&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;int32&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; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nan_mask&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;isnan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;as_bf16&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;ok&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;up_bits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; want&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; nan_mask&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;up-cast bit check: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;ok&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;span&gt;item&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; &#x2F; 65536 patterns match &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;(pattern &amp;lt;&amp;lt; 16)&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;  of the 65536, &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;nan_mask&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;span&gt;item&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; are NaN codes, &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;checked separately:&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;nan_ok&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;isnan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;as_bf16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()[&lt;&#x2F;span&gt;&lt;span&gt;nan_mask&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]).&lt;&#x2F;span&gt;&lt;span&gt;all&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-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;  every NaN pattern up-casts to a float32 NaN: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;nan_ok&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&gt;exact&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;up_bits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; want&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;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-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;  bit-exact including NaN payloads: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;exact&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F; 65536&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;# ---- down: fp32 -&amp;gt; bf16 is &amp;quot;keep the top 16 bits, round by the rest&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;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; truncate_rne&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;f32&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;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;The hand rule: add the rounding bias, then keep the top half.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;    bias = 0x7FFF + (bit 16), the classic nearest-even truncation.&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&gt;    u&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; f32&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;int32&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;    bias&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-storage z-type&quot;&gt; 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;7FFF&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; ((&lt;&#x2F;span&gt;&lt;span&gt;u&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 16&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; &amp;amp;&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;    out&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;u&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; bias&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; &amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;to&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;int16&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;bfloat16&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;    return&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;where&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;isnan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;f32&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;nan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; out&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&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;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;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;1_000_000&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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;logspace&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;38&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; 38&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_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;edges&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;0.0&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;0.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; 1.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; 1.0078125&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; 65504.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.4e38&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;                      1e-40&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt; float&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;inf&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; float&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;-inf&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; float&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;nan&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;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;cat&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; edges&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;ours&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; truncate_rne&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;torchs&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;bfloat16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;float&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;agree&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;ours&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; torchs&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-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;isnan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ours&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; &amp;amp;&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;isnan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;torchs&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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;down-cast rule check: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;agree&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;span&gt;item&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; &#x2F; &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&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;x&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;values agree with torch&amp;#39;s cast&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 picture, on one number&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; 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;1.7014&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;u32&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;view&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;int32&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;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt; &amp;amp; 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;FFFFFFFF&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;b16&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;bfloat16&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;int16&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;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt; &amp;amp; 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;FFFF&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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;1.7014 in float32 bits : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;u32&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:032b&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;1.7014 in bfloat16 bits: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;b16&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:016b&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; (the top half, rounded)&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;bfloat16 value stored  : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;bfloat16&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;span class=&quot;z-storage z-type&quot;&gt;:.6f&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p17_bf16_truncation.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;Where it runs today: TPUs since their second generation &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-23&quot;&gt;23&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;,
NVIDIA GPUs since the A100 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-20&quot;&gt;20&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, and this machine: torch&#x27;s CPU
autocast picks bfloat16 by default
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p16-wide-formats&quot;&gt;proof&lt;&#x2F;a&gt; prints it). In large-model
training it is the usual compute half of the mixed-precision
loop, with FP8 taking over the matmuls on the newest chips (the
FP8 section returns to this).&lt;&#x2F;p&gt;
&lt;p&gt;How it fails: 2 digits. 1.001 stores as exactly 1.0
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p7-sixteen-bits&quot;&gt;proof&lt;&#x2F;a&gt;). The step at 1.0 is 0.0078, so
a healthy update of 0.001 sits under half a step and rounds
away: the disaster that opens the training sections ahead, and
the reason master weights exist. bfloat16 did not make training precise. It
made training&#x27;s failures the quiet, repairable kind, and the
machinery that repairs them is most of the rest of this page.&lt;&#x2F;p&gt;
&lt;p&gt;The guide pauses here, because at 8 bits and below no format
stands alone. Each narrow format arrives later on this page, at
the moment the training story needs it, and a closing sheet
gathers the whole family in one drawing. First, a payoff: with
the wide formats on the table, the small constants in every
model&#x27;s source code finally have their explanation.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-little-epsilons-explained&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-little-epsilons-explained&quot; aria-label=&quot;Anchor link for: the-little-epsilons-explained&quot;&gt;The little epsilons, explained&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Open any model&#x27;s source code and small constants appear: an
&lt;code&gt;eps=1e-5&lt;&#x2F;code&gt; in the layer norm, an &lt;code&gt;eps=1e-8&lt;&#x2F;code&gt; in Adam, a
&lt;code&gt;logits - logits.max()&lt;&#x2F;code&gt; in every attention implementation. None of them is superstition; each one is a patch over a place
where a formula&#x27;s intermediate value leaves the map.&lt;&#x2F;p&gt;
&lt;p&gt;The largest number float32 can hold is about 3.4e38, and exp
reaches it at 88.73 (measured below). Softmax, the function that
turns a model&#x27;s raw output scores, its logits, into
probabilities, applies exp to every logit. Logits above 88.73
exist in every large model, so a naive softmax returns inf, then
inf divided by inf, and your loss prints nan. The repair
costs nothing: softmax only sees differences, so subtract the
maximum first and the largest input becomes 0
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p6-stability&quot;&gt;proof&lt;&#x2F;a&gt;):&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 658px;&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;chfp&#x2F;drawings&#x2F;softmax-cliff-light.svg?h=d4c49ab4b305a9bad332&quot;
         alt=&quot;the exp curve climbing toward float32&amp;#x27;s dashed ceiling at 3.4e38 and clipping at a red vertical line at z equals 88.7, exp of 88.8 equals inf; a dashed red line marks float16&amp;#x27;s cliff at z equals 11.09; 3 red ticks near 120 are your logits, past the cliff; an orange arrow slides them to landed ticks at 0 and below; subtract the max, the answer unchanged, softmax only sees differences&quot; width=&quot;658&quot; height=&quot;289&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;chfp&#x2F;drawings&#x2F;softmax-cliff-dark.svg?h=960d47151dc7efe135bd&quot;
         alt=&quot;the exp curve climbing toward float32&amp;#x27;s dashed ceiling at 3.4e38 and clipping at a red vertical line at z equals 88.7, exp of 88.8 equals inf; a dashed red line marks float16&amp;#x27;s cliff at z equals 11.09; 3 red ticks near 120 are your logits, past the cliff; an orange arrow slides them to landed ticks at 0 and below; subtract the max, the answer unchanged, softmax only sees differences&quot; width=&quot;658&quot; height=&quot;289&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 cliff is at 88.7 in float32 and at 11.09 in float16. every stable softmax you have ever used is the same one-line slide.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p6-stability&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;p6_stability.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 stability patterns. exp overflows at 88.73 in float32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;and 11.09 in float16, so a naive softmax dies on logits your model&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;produces every day; subtracting the max moves nothing and fixes it&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;(softmax is shift-invariant).&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;float32 exp cliff: exp(88.7)=&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&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;88.7&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;exp&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;span class=&quot;z-storage z-type&quot;&gt;:.3e&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;exp(88.8)=&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&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;88.8&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;exp&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;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&gt;h&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;11.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; 11.1&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;float16&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;float16 exp cliff: exp(11.0)=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;h&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;exp&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;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;, exp(11.1)=&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;h&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;exp&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;item&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;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;logits&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;120.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; 119.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; 115.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;naive&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; logits&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;exp&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; logits&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;exp&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;span&gt;stable&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;logits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; logits&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()).&lt;&#x2F;span&gt;&lt;span&gt;exp&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-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;logits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; logits&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()).&lt;&#x2F;span&gt;&lt;span&gt;exp&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;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;naive  softmax([120,119,115]) = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;naive&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tolist&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;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;stable softmax([120,119,115]) = &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 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;v&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-keyword&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; stable&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tolist&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;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.softmax agrees with stable: &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;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;allclose&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;softmax&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;logits&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; stable&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;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;# logsumexp, same illness, same cure&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;naive  log(sum(exp)) = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;logits&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;exp&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;span&gt;log&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;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;stable logsumexp     = &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&gt;logsumexp&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;logits&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p6_stability.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;The same pattern explains the rest of the family. &lt;code&gt;logsumexp&lt;&#x2F;code&gt;
slides by the max for the same reason. &lt;code&gt;log1p(x)&lt;&#x2F;code&gt; and &lt;code&gt;expm1(x)&lt;&#x2F;code&gt;
exist because near zero, 1 + x absorbs x (the absorption figure
again, at 1.0 instead of 1e8). The layer-norm epsilon keeps a
near-zero variance from turning &lt;code&gt;rsqrt&lt;&#x2F;code&gt; into inf; Adam&#x27;s epsilon
does the same for its denominator; and an attention mask uses a
large negative number so that after the slide it underflows to a
clean zero instead of poisoning the row with nan. One picture,
many patches: keep the intermediate values where the points are.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-smaller-floats&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#why-smaller-floats&quot; aria-label=&quot;Anchor link for: why-smaller-floats&quot;&gt;Why smaller floats&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The guide paused at 16 bits. Here is why the family keeps
going down anyway.&lt;&#x2F;p&gt;
&lt;p&gt;Training a network is mostly moving numbers, not multiplying
them: weights travel from memory to the arithmetic and gradients
travel back, billions per step, and the wire is slower than the
multiplier. Halve the bits per number and the same wire carries
twice the numbers, the same memory holds twice the model, and the
tensor cores, built to process the narrower type, double their
arithmetic too. Here is the wire, drawn to scale:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 762px;&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;chfp&#x2F;drawings&#x2F;cpu-gpu-pcie-light.svg?h=e83a6f5d157fcc84dcd3&quot;
         alt=&quot;the chips and the pipes between them, widths drawn to one bandwidth scale: a CPU and system RAM box joined to the GPU by a thin PCIe 5.0 x16 line, 64 GB per second each way, where the weights cross once at load; the HBM3 box holding the weights and the KV cache, joined to the GPU die and its tensor cores by a wide orange pipe, 3,350 GB per second, 52 times the PCIe pipe; every training step and every generated token pulls the weights through the wide pipe&quot; width=&quot;762&quot; height=&quot;273&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;chfp&#x2F;drawings&#x2F;cpu-gpu-pcie-dark.svg?h=609453978a644147d606&quot;
         alt=&quot;the chips and the pipes between them, widths drawn to one bandwidth scale: a CPU and system RAM box joined to the GPU by a thin PCIe 5.0 x16 line, 64 GB per second each way, where the weights cross once at load; the HBM3 box holding the weights and the KV cache, joined to the GPU die and its tensor cores by a wide orange pipe, 3,350 GB per second, 52 times the PCIe pipe; every training step and every generated token pulls the weights through the wide pipe&quot; width=&quot;762&quot; height=&quot;273&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 31.&lt;&#x2F;span&gt; the pipes, to one scale (an H100&#x27;s numbers [30]). the thin line is how the weights arrive; the wide pipe is what every step and every token pays. the byte ladders ahead shrink what crosses it.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The only question is whether the surviving points still cover
the numbers the network produces with small enough error.
Networks tolerate rounding noise unusually well: they are
trained on noisy batches and judged over many outputs, so the
answer stays yes far below 32 bits if the format is chosen
carefully. The fork figure already showed the first halving&#x27;s
two options, and training mostly picked bfloat16: reach first.
The cost is a step of 0.0078 at 1.0, and the next section shows
what that step does to learning.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-update-that-vanished&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-update-that-vanished&quot; aria-label=&quot;Anchor link for: the-update-that-vanished&quot;&gt;The update that vanished&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;At 16 bits the map&#x27;s steps are wide enough to stop training
itself. A bfloat16 weight sitting at 1.0 has neighbors
0.0078 away. A healthy update of 0.001 is an arrow one eighth of
that step:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 692px;&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;chfp&#x2F;drawings&#x2F;lost-update-light.svg?h=9bb678cdb7c3517a37ea&quot;
         alt=&quot;a zoomed bfloat16 interval between 1.0000 and 1.0078; an orange update arrow of 0.001 reaches an eighth of the way, short of the dashed halfway mark, and a dashed return path brings it back: x 1,000 updates, still exactly 1.0; below, stochastic rounding splits the same update: a tall grey bar, 87 percent stays, and a thin dashed arrow jumping the whole step, 13 percent to 1.0078; 0.87 times 0 plus 0.13 times 0.0078 equals 0.001, on average the weight still moves&quot; width=&quot;692&quot; height=&quot;347&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;chfp&#x2F;drawings&#x2F;lost-update-dark.svg?h=984e78ddb8b44f197535&quot;
         alt=&quot;a zoomed bfloat16 interval between 1.0000 and 1.0078; an orange update arrow of 0.001 reaches an eighth of the way, short of the dashed halfway mark, and a dashed return path brings it back: x 1,000 updates, still exactly 1.0; below, stochastic rounding splits the same update: a tall grey bar, 87 percent stays, and a thin dashed arrow jumping the whole step, 13 percent to 1.0078; 0.87 times 0 plus 0.13 times 0.0078 equals 0.001, on average the weight still moves&quot; width=&quot;692&quot; height=&quot;347&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 32.&lt;&#x2F;span&gt; the update that vanished, and the repair. deterministic rounding silently stops training for that weight; stochastic rounding keeps it moving on average.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The proof runs it: 1,000 consecutive updates of 0.001
applied to a bfloat16 weight leave it at exactly 1.0
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p8-lost-update&quot;&gt;proof&lt;&#x2F;a&gt;). This is why every mixed-precision
recipe since 2017 keeps a float32 master copy of the weights: the
forward and backward passes run narrow, but the optimizer adds the
update into a copy whose local step is 65536 times finer, where
0.001 lands easily &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-5&quot;&gt;5&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The other repair is
stochastic rounding, which we will need again at 4 bits: round
up with probability proportional to how far you got. In the same
proof, the stochastically rounded weight reaches 2.0 alongside the
master copy.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p8-lost-update&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;p8_lost_update.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 update that vanished. In bf16, a weight near 1.0 has a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;local step of about 0.0078; an update smaller than half that step&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;rounds away, and training silently stops for that weight. The fp32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;master copy is the fix; stochastic rounding is the other fix.&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&gt;w&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;1.0&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;bfloat16&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;u&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; 1e-3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;                                   # lr * grad, a normal size&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;bf16 next after 1.0 : &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&gt;nextafter&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;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;1.0&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;bfloat16&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;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;2.0&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;bfloat16&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;span class=&quot;z-storage z-type&quot;&gt;:.6f&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;w + &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;u&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; in bf16     : &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;w&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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&gt;u&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;bfloat16&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;span class=&quot;z-storage z-type&quot;&gt;:.6f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;  (unchanged: &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;w&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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&gt;u&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;bfloat16&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; w&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;# 1000 such updates, three ways&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;steps&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; 1000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;wb&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;1.0&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;bfloat16&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;steps&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;    wb&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; wb&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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&gt;u&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;bfloat16&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;wm&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;1.0&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;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;     # master copy&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;steps&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;    wm&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; wm&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; u&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;g&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;Generator&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;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;            # stochastic rounding, emulated&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ws&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;1.0&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 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;steps&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;    hi&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;nextafter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ws&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;to&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;bfloat16&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;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;2.0&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;bfloat16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span&gt;float&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;    lo&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ws&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;to&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;bfloat16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&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;    x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ws&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; u&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 z-punctuation&quot;&gt; = ((&lt;&#x2F;span&gt;&lt;span&gt;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; lo&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-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;hi&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; lo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span&gt;clamp&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; 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;    ws&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; hi&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&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;rand&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;((),&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; generator&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;g&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; &amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; p&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; lo&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;after &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;steps&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; updates of &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;u&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;  bf16 accumulate    : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;wb&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   (true answer 2.0)&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;  fp32 master        : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;wm&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&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;  bf16 + stochastic  : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;ws&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p8_lost_update.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;So the loop that trains every model you use runs like this:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 878px;&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;chfp&#x2F;drawings&#x2F;loop-stations-light.svg?h=f2641f8ad4fd63b7488a&quot;
         alt=&quot;the mixed-precision training loop as 10 stations in a cycle: master weights in float32, cast down to a narrow copy drawn as a 32-bit bar funneling into a 16-bit bar, matmuls with narrow inputs and a float32 accumulator, narrow activations, loss in float32, multiply the loss by S for float16, backward pass with narrow gradients, divide by S with a red diamond asking inf anywhere, yes: skip the step, the Adam moments m and v as two 32-bit chips, and the update landing in float32 before the loop closes; each station carries a bar of its numbers&amp;#x27; bits, 1 px per bit&quot; width=&quot;878&quot; height=&quot;386&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;chfp&#x2F;drawings&#x2F;loop-stations-dark.svg?h=0bd4b86bcd09deb83735&quot;
         alt=&quot;the mixed-precision training loop as 10 stations in a cycle: master weights in float32, cast down to a narrow copy drawn as a 32-bit bar funneling into a 16-bit bar, matmuls with narrow inputs and a float32 accumulator, narrow activations, loss in float32, multiply the loss by S for float16, backward pass with narrow gradients, divide by S with a red diamond asking inf anywhere, yes: skip the step, the Adam moments m and v as two 32-bit chips, and the update landing in float32 before the loop closes; each station carries a bar of its numbers&amp;#x27; bits, 1 px per bit&quot; width=&quot;878&quot; height=&quot;386&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 33.&lt;&#x2F;span&gt; one training step as it actually runs, all 10 stations. narrow bits where the volume is; float32 where errors accumulate; the S dial and the red inf check guard the float16 path.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The products accumulate in float32 inside the tensor cores: that
is the fused multiply-add from the rounding section doing its
job. Precision goes exactly where the numbers need it and
nowhere else. Stations 6 and 8, the S dial and the inf check,
are the subject of the next section.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;loss-scaling&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#loss-scaling&quot; aria-label=&quot;Anchor link for: loss-scaling&quot;&gt;Loss scaling&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;One group of numbers still fails inside this loop. Gradients
are the smallest numbers in training, and float16&#x27;s floor is
high: its
subnormals end near 6e-8 and its normal range starts at 6e-5.
The left tail of the gradient distribution simply falls below
every point of the format:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 726px;&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;chfp&#x2F;drawings&#x2F;scaling-slide-light.svg?h=b0428d0df8350c6e3f65&quot;
         alt=&quot;the measured gradient histogram per decade drawn twice over one log axis: dashed grey as it is, its small tail sitting on the float16 subnormal ramp, and solid orange after multiplying by 1024, 10 doublings right, fully inside float16&amp;#x27;s normal zone; the axis carries 3 zones: 0 in float16 below 6e-8, the coarse subnormal ramp to 6e-5, and the normal points beyond; bar heights are percent of the measured gradients per decade on a square-root scale&quot; width=&quot;726&quot; height=&quot;387&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;chfp&#x2F;drawings&#x2F;scaling-slide-dark.svg?h=01e6948f6cc4d5ff1b6d&quot;
         alt=&quot;the measured gradient histogram per decade drawn twice over one log axis: dashed grey as it is, its small tail sitting on the float16 subnormal ramp, and solid orange after multiplying by 1024, 10 doublings right, fully inside float16&amp;#x27;s normal zone; the axis carries 3 zones: 0 in float16 below 6e-8, the coarse subnormal ramp to 6e-5, and the normal points beyond; bar heights are percent of the measured gradients per decade on a square-root scale&quot; width=&quot;726&quot; height=&quot;387&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 34.&lt;&#x2F;span&gt; the histogram meets the map. multiplying the loss by one number slides every gradient into the band; dividing afterward puts them back.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The rescue is one multiplication. Scale the loss by S before the
backward pass and, because differentiation is linear, every
gradient arrives multiplied by S; unscale after casting and
nothing has changed except which gradients survived. In practice S adapts:
overflow detected, halve it and skip the step; a run of clean
steps, double it. That is the whole of &lt;code&gt;GradScaler&lt;&#x2F;code&gt;
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p9-loss-scaling&quot;&gt;proof&lt;&#x2F;a&gt; measures the cast; on our toy
net only 0.1% of gradients die, and the fraction grows with depth
and training time, which is why the recipe exists
&lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-5&quot;&gt;5&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;). bfloat16 mostly
retired this machinery: it keeps float32&#x27;s reach, so its normal
floor sits at 1.2e-38 against float16&#x27;s 6.1e-5, about 34 powers
of 10 lower, and the scaler became optional.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p9-loss-scaling&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;p9_loss_scaling.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 scaling, measured. Take real float32 gradients from a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;small net, cast them to float16 as mixed precision must, and count&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 ones that flush to zero; then scale by 1024 first, cast, unscale.&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 cast is where gradients die; the scale is the rescue.&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;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;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;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;net&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;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; 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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&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; 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; 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;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;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; 64&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-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 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&gt;prod&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-variable&quot;&gt; keepdim&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;loss&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;net&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 class=&quot;z-keyword z-operator&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 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;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;mean&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-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;backward&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;g&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;cat&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&gt;grad&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;flatten&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; p&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; net&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;parameters&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;nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; g&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;g&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; 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;plain&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;nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;half&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; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;mean&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;scaled&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;nz&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; 1024&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;half&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;float&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; ==&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;float&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;mean&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;nonzero float32 gradients            : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&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;nz&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;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;flushed to zero by the float16 cast  : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;plain&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.1%&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;same cast after scaling by 1024      : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;scaled&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.1%&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;smallest surviving gradient          : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;nz&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;min&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;span class=&quot;z-storage z-type&quot;&gt;:.2e&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;float16&amp;#39;s smallest subnormal         : &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&gt;finfo&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;float16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;smallest_normal&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;span class=&quot;z-keyword z-operator&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-storage z-type&quot;&gt;:.2e&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p9_loss_scaling.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;fp8&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#fp8&quot; aria-label=&quot;Anchor link for: fp8&quot;&gt;FP8&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Halve again and there are not enough bits left for one format
to serve both directions of training. So at 8 bits the fork
appears one more time, and this time both forks ship, as a
pair &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-6&quot;&gt;6&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;: E4M3 keeps digits, E5M2 keeps reach.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 777px;&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;chfp&#x2F;drawings&#x2F;e4m3-card-light.svg?h=e0cf763a8c1ad0f5979c&quot;
         alt=&quot;the bit fields of FP8 E4M3 drawn to scale: 1 sign bit, 4 exponent bits in orange, 3 mantissa bits, 1 byte per number; a card lists bias 7, ceiling 448, normal floor 0.0156, ramp floor 0.00195, step at 1.0 of 0.125 highlighted, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;777&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;e4m3-card-dark.svg?h=cc70a9173a1fe01a0f66&quot;
         alt=&quot;the bit fields of FP8 E4M3 drawn to scale: 1 sign bit, 4 exponent bits in orange, 3 mantissa bits, 1 byte per number; a card lists bias 7, ceiling 448, normal floor 0.0156, ramp floor 0.00195, step at 1.0 of 0.125 highlighted, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;777&quot; height=&quot;342&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 35.&lt;&#x2F;span&gt; E4M3&#x27;s card. the map section already drew all 256 of its patterns; this is the same format as a card.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;E4M3 you have known since the map section: its 15 intervals are
this page&#x27;s teaching map, and its 256 patterns are decoded in
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p2-e4m3-map&quot;&gt;proof&lt;&#x2F;a&gt;. The card records its one non-IEEE
choice: no infinity. Those codes were used for one more
doubling of range, overflow stops at 448, and a single pattern
means NaN.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 772px;&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;chfp&#x2F;drawings&#x2F;e5m2-card-light.svg?h=e55f8ffb4ce5556263e3&quot;
         alt=&quot;the bit fields of FP8 E5M2 drawn to scale: 1 sign bit, 5 exponent bits in orange, 2 mantissa bits, 1 byte per number; a card lists bias 15, ceiling 57344 highlighted, normal floor 6.1e-5, ramp floor 1.53e-5, step at 1.0 of 0.25, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;772&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;e5m2-card-dark.svg?h=561708a8f95e57dd8220&quot;
         alt=&quot;the bit fields of FP8 E5M2 drawn to scale: 1 sign bit, 5 exponent bits in orange, 2 mantissa bits, 1 byte per number; a card lists bias 15, ceiling 57344 highlighted, normal floor 6.1e-5, ramp floor 1.53e-5, step at 1.0 of 0.25, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;772&quot; height=&quot;342&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 36.&lt;&#x2F;span&gt; E5M2&#x27;s card: float16&#x27;s 5 exponent bits kept, 8 of its 10 mantissa bits cut.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;E5M2 is float16 with the bottom byte cut, and that is a
bit-level fact, provable the same way bfloat16 was: all 256
E5M2 patterns cast to float16 as the same 8 bits with 8 zeros
appended (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p20-e5m2-top-byte&quot;&gt;proof&lt;&#x2F;a&gt; checks every one).&lt;&#x2F;p&gt;
&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;chfp&#x2F;drawings&#x2F;e5m2-map-light.svg?h=a6716102293dc54cef03&quot;
         alt=&quot;the E5M2 map drawn whole: a dashed ramp interval and 30 power of two intervals on one unbroken line, each holding the same 4 points, from 6.1e-5 to 57344 with inf past the ceiling; below, the interval from 1 to 2 showing all 4 of its points with a step bracket, 2 to the -2 equals 0.25, and under it a dense tick row: float16 in the same interval has 1,024 points&quot; width=&quot;892&quot; height=&quot;353&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;chfp&#x2F;drawings&#x2F;e5m2-map-dark.svg?h=fb59f4bfd7eb357d189d&quot;
         alt=&quot;the E5M2 map drawn whole: a dashed ramp interval and 30 power of two intervals on one unbroken line, each holding the same 4 points, from 6.1e-5 to 57344 with inf past the ceiling; below, the interval from 1 to 2 showing all 4 of its points with a step bracket, 2 to the -2 equals 0.25, and under it a dense tick row: float16 in the same interval has 1,024 points&quot; width=&quot;892&quot; height=&quot;353&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 37.&lt;&#x2F;span&gt; the E5M2 map: float16&#x27;s 30 intervals with 4 points in each. reach kept, digits nearly gone.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p20-e5m2-top-byte&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;p20_e5m2_top_byte.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: FP8 E5M2 is float16&amp;#39;s top byte, at the bit level. The&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;same fact as bfloat16 being float32&amp;#39;s top half, one floor down:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;E5M2 keeps float16&amp;#39;s 5 exponent bits and the top 2 of its 10&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;mantissa bits. Cast any of the 256 E5M2 bit patterns to float16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;and the result is the same 8 bits with 8 zeros appended.&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pats&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;256&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;uint8&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;as_e5m2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pats&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float8_e5m2&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;up_bits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; as_e5m2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;half&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;int16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt; &amp;amp; 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;FFFF&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;want&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pats&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;to&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;int16&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; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nan_mask&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;isnan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;as_e5m2&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;ok&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;up_bits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; want&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; nan_mask&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;up-cast bit check: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;ok&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;span&gt;item&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; &#x2F; 256 patterns match &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;(pattern &amp;lt;&amp;lt; 8)&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;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;  {&lt;&#x2F;span&gt;&lt;span&gt;nan_mask&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;span&gt;item&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; NaN codes, all up-cast to &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;float16 NaN: &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&gt;isnan&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;as_e5m2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;half&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()[&lt;&#x2F;span&gt;&lt;span&gt;nan_mask&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]).&lt;&#x2F;span&gt;&lt;span&gt;all&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;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&gt;exact&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;up_bits&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; want&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;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-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;  bit-exact including NaN payloads: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;exact&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; &#x2F; 256&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;v&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;1.7014&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;float16&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;b8&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;to&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;float8_e5m2&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&gt;torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;uint8&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;u16&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;view&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;int16&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;span class=&quot;z-keyword z-operator z-storage z-type&quot;&gt; &amp;amp; 0x&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;FFFF&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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;1.7014 in float16 bits: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;u16&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:016b&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;1.7014 in E5M2 bits   : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;b8&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:08b&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; (the top byte, rounded)&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;E5M2 value stored     : &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;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;to&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;float8_e5m2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p20_e5m2_top_byte.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;NVIDIA, Arm and Intel proposed the pair for deep learning in
2022 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-6&quot;&gt;6&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, and hardware arrived with the Hopper chips: E4M3
carries the forward pass, E5M2 carries the gradients, whose
tails need reach more than digits. Each tensor also carries a
float32 scale, chosen from the tensor&#x27;s recent maximum so that
its histogram slides into the band: exactly the loss-scaling
picture, applied per tensor. Recipes differ only in whether the
maximum is tracked from history (delayed scaling) or measured
on the spot:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 682px;&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;chfp&#x2F;drawings&#x2F;delayed-scaling-light.svg?h=9ceec455c97ff91e01d8&quot;
         alt=&quot;5 bars show a tensor&amp;#x27;s largest value step by step from t-4 to t under a bracket labeled the amax history window; a dashed level line touches the tallest bar, the max, and an arrow leads from it to a box reading delayed: reuse the window&amp;#x27;s max, free; a second orange arrow leads from bar t itself to a box reading current: measure step t itself, 1 extra pass&quot; width=&quot;682&quot; height=&quot;219&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;chfp&#x2F;drawings&#x2F;delayed-scaling-dark.svg?h=4b9d2495e4bf7edb41f2&quot;
         alt=&quot;5 bars show a tensor&amp;#x27;s largest value step by step from t-4 to t under a bracket labeled the amax history window; a dashed level line touches the tallest bar, the max, and an arrow leads from it to a box reading delayed: reuse the window&amp;#x27;s max, free; a second orange arrow leads from bar t itself to a box reading current: measure step t itself, 1 extra pass&quot; width=&quot;682&quot; height=&quot;219&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 38.&lt;&#x2F;span&gt; where a tensor&#x27;s FP8 scale comes from: the loss-scaling dial, automated per tensor.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;And not everything is quantized: attention&#x27;s and
the MLP&#x27;s big matmuls run in FP8, while softmax, the
normalizations, the first embedding and the final projection
stay wide &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-11&quot;&gt;11&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Wired into a transformer through NVIDIA&#x27;s Transformer Engine,
the published result is training runs 30 to 40 percent faster
at matching loss curves &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-6&quot;&gt;6&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, and DeepSeek-V3 trained a
671-billion-parameter model with FP8 matmuls by scaling
128-element tiles instead of whole tensors &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-7&quot;&gt;7&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, a step toward
what comes next. One caution before the next halving: E4M3
carries about 1.2 decimal digits and E5M2 about 0.9, so at 8
bits no single stored number is trustworthy; only averages over
many are.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-outlier-problem&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-outlier-problem&quot; aria-label=&quot;Anchor link for: the-outlier-problem&quot;&gt;The outlier problem&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Before the next halving, meet the format waiting at the bottom,
because its 2 numbers explain everything that follows. 4 bits:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 780px;&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;chfp&#x2F;drawings&#x2F;e2m1-card-light.svg?h=9c9d281846e5e5440dc3&quot;
         alt=&quot;the bit fields of FP4 E2M1 drawn to scale: 1 sign bit, 2 exponent bits in orange, 1 mantissa bit, 4 bits per number; a card lists bias 1, ceiling 6 highlighted, normal floor 1, ramp floor 0.5, step at 1.0 of 0.5, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;780&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;e2m1-card-dark.svg?h=84491fc5b0c692aee499&quot;
         alt=&quot;the bit fields of FP4 E2M1 drawn to scale: 1 sign bit, 2 exponent bits in orange, 1 mantissa bit, 4 bits per number; a card lists bias 1, ceiling 6 highlighted, normal floor 1, ramp floor 0.5, step at 1.0 of 0.5, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;780&quot; height=&quot;342&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 39.&lt;&#x2F;span&gt; E2M1&#x27;s card. 16 codes; the next drawing shows every one of them.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 709px;&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;chfp&#x2F;drawings&#x2F;e2m1-sixteen-light.svg?h=d1e63963e704d2397519&quot;
         alt=&quot;every value of the 4-bit E2M1 float on one line: minus 6 to 6 through minus and plus 0.5, 1, 1.5, 2, 3, 4 and 6, 15 ticks with the zero tick labeled plus and minus 0, the negative half grey and the positive half orange; step brackets under the axis read step 0.5, step 1 and step 2, and an orange bracket spans 0.5 to 6, the whole positive range is 6 over 0.5 equals 12x&quot; width=&quot;709&quot; height=&quot;215&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;chfp&#x2F;drawings&#x2F;e2m1-sixteen-dark.svg?h=4db9a360872866bf34ae&quot;
         alt=&quot;every value of the 4-bit E2M1 float on one line: minus 6 to 6 through minus and plus 0.5, 1, 1.5, 2, 3, 4 and 6, 15 ticks with the zero tick labeled plus and minus 0, the negative half grey and the positive half orange; step brackets under the axis read step 0.5, step 1 and step 2, and an orange bracket spans 0.5 to 6, the whole positive range is 6 over 0.5 equals 12x&quot; width=&quot;709&quot; height=&quot;215&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 40.&lt;&#x2F;span&gt; every value of a 4-bit E2M1 float: 15 numbers plus a second zero. you have now seen all of them.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;16 codes, 15 values (0 appears twice, once with each sign), no
inf, no NaN, and a total width of 6 &#x2F; 0.5 = 12x, about 3.6
doublings. The 2023 OCP standard defined E2M1 as the 4-bit
element of the block family these sections are building &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-9&quot;&gt;9&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, and the
newest NVIDIA chips run it at twice the FP8 rate &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-24&quot;&gt;24&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. Alone it
is unusable, and even a per-tensor scale cannot save it,
because per-tensor scaling has one enemy. Transformer
activations grow a few channels whose values run hundreds of
times larger than the rest, systematically, past about 6
billion parameters &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-8&quot;&gt;8&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. One scale must now serve the spike and
the bell at once:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 742px;&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;chfp&#x2F;drawings&#x2F;outlier-panels-light.svg?h=4cb13c6f46e6a7e6bd9c&quot;
         alt=&quot;3 panels each with a bell histogram and one tall orange spike; under each, a proportion bar shows how much of the bell quantizes to 0: all of it for FP4 with one tensor scale, 31 percent for FP4 with a scale per 32, 1 percent for FP8 with one tensor scale; below each, a mini map of where that scale puts the format&amp;#x27;s points, with a dashed line from the spike pinning the top end&quot; width=&quot;742&quot; height=&quot;293&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;chfp&#x2F;drawings&#x2F;outlier-panels-dark.svg?h=bbe84c5d6ade41effad4&quot;
         alt=&quot;3 panels each with a bell histogram and one tall orange spike; under each, a proportion bar shows how much of the bell quantizes to 0: all of it for FP4 with one tensor scale, 31 percent for FP4 with a scale per 32, 1 percent for FP8 with one tensor scale; below each, a mini map of where that scale puts the format&amp;#x27;s points, with a dashed line from the spike pinning the top end&quot; width=&quot;742&quot; height=&quot;293&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 41.&lt;&#x2F;span&gt; one spike, three formats, measured. FP8 loses 1%; FP4 with one scale loses the entire bell; blocks are the rescue.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Read the measured numbers. FP8 with a single scale loses 1% of
the ordinary values next to a 500x outlier, because a float grid
keeps its relative error at any scale until values fall below
its smallest positive number, and FP8&#x27;s smallest is 229,376x
under its largest (448 &#x2F; 0.00195, from the map section). FP4&#x27;s
smallest is only 12x under its largest (6 &#x2F; 0.5, computed
above). One spike takes the whole 12x, and
100% of the ordinary values quantize to zero
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p10-fp4-outlier&quot;&gt;proof&lt;&#x2F;a&gt;). At 4 bits, blocks are not an
improvement; nothing works without them.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p10-fp4-outlier&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;p10_fp4_outlier.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: why blocks had to be invented. A float grid keeps its&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;relative precision at any scale, so FP8 with one per-tensor scale&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;survives an outlier surprisingly well. FP4 does not: its largest&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;positive value is 6 and its smallest is 0.5, only 12x apart, so one&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;spike pushes every ordinary value below the smallest, and the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;tensor quantizes to zeros. A scale per 32 elements&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;rescues it.&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-constant&quot;&gt;E2M1&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;0.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.5&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.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; 1.5&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.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.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; 4.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; 6.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-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; round_e2m1&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;v&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;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;Round |v| to the nearest E2M1 magnitude, keep the sign.&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&gt;    idx&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;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;unsqueeze&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; E2M1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;argmin&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;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; E2M1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;idx&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;sign&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-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; quant&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;x&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; block&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;    q&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;empty_like&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;&#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 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-support&quot;&gt; len&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; block&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; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;block&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;        s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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; 6.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;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-keyword&quot;&gt; else&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;1.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;        q&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;block&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-keyword z-operator z-assignment&quot;&gt;] =&lt;&#x2F;span&gt;&lt;span&gt; round_e2m1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; s&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; s&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&gt; q&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;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;128&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; 0.1&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-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;40&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; 50.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mask&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;128&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;bool&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span&gt;; mask&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&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-language&quot;&gt; False&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;for&lt;&#x2F;span&gt;&lt;span&gt; name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; block&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;per-tensor&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; 128&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;per-block-32&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; 32&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;    q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; quant&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; block&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;    zeros&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;q&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;mask&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; 0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;mean&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;    rel&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;q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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;mask&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;abs&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; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;mask&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()).&lt;&#x2F;span&gt;&lt;span&gt;median&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;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:14s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; ordinary values quantized to zero: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;zeros&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;   &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;median rel err of the rest: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;rel&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.1%&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&gt;q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; quant&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 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 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;the outlier itself survives either way: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;q&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&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;:.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;&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;&#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;the same tensor in FP8 E4M3, one per-tensor scale, for honesty:&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;s&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;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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; 448.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;q8&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;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; s&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;to&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;float8_e4m3fn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&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; s&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;per-tensor FP8    zeros: &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;q8&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;mask&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;0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;float&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;mean&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;:.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;   &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;median rel err: &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;q8&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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;mask&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;abs&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;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;mask&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;].&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;()).&lt;&#x2F;span&gt;&lt;span&gt;median&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;:.1%&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-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;a float grid holds its relative error at any scale, until its&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;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;smallest positive value; FP4&amp;#39;s smallest (0.5) is only 12x below&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;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;its largest (6), and that is the whole reason microscaling exists.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p10_fp4_outlier.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;microscaling&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#microscaling&quot; aria-label=&quot;Anchor link for: microscaling&quot;&gt;Microscaling&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;So the scale moved into the data type. The Open Compute Project&#x27;s
MX formats, standardized in 2023 by AMD, Arm, Intel, Meta,
Microsoft, NVIDIA and Qualcomm &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-9&quot;&gt;9&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, cut every tensor
into blocks of 32 and give each block one 8-bit scale:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&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;chfp&#x2F;drawings&#x2F;mx-anatomy-light.svg?h=62cb5b3e6a15e7254c61&quot;
         alt=&quot;an MX block: an orange E8M0 scale chip, its own 8 bits drawn all exponent, an interval picker with no position, bracketed over 32 E2M1 element cells, 32 times 4 plus 8 equals 136 bits, 4.25 per number; an E2M1 bit bar drawn beside it for contrast; below, the memory lane: 1 scale byte then 16 element bytes, 2 elements in each, consumed whole by the tensor core&quot; width=&quot;752&quot; height=&quot;307&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;chfp&#x2F;drawings&#x2F;mx-anatomy-dark.svg?h=9b8bedcbcd330d08ca7f&quot;
         alt=&quot;an MX block: an orange E8M0 scale chip, its own 8 bits drawn all exponent, an interval picker with no position, bracketed over 32 E2M1 element cells, 32 times 4 plus 8 equals 136 bits, 4.25 per number; an E2M1 bit bar drawn beside it for contrast; below, the memory lane: 1 scale byte then 16 element bytes, 2 elements in each, consumed whole by the tensor core&quot; width=&quot;752&quot; height=&quot;307&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 42.&lt;&#x2F;span&gt; the MX block. 32 elements, one shared scale, consumed whole by the tensor cores. the scale is an exponent with no mantissa: an interval-picker and nothing else.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Every idea on this page meets here. The E8M0 scale is a float
with no mantissa at all, the interval choice from the floating-dot
figure with the position deleted, so applying it is exponent
addition, free in silicon. The 32-element block is the histogram
cut fine enough that an outlier only damages its own block.
The accounting is 136 bits per 32 numbers: 4.25 bits each,
136 &#x2F; 128 = 1.06, so 6% more than raw 4-bit storage. And the block size is a measured compromise: smaller blocks
mean less error and more storage spent on scales, and our own
sweep across sizes 8, 16, 32, 64, 128 shows both moving (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p11-mxblock&quot;&gt;proof&lt;&#x2F;a&gt;). The same
chassis carries 8-, 6- and 4-bit elements as MXFP8, MXFP6,
MXFP4.&lt;&#x2F;p&gt;
&lt;p&gt;What the tensor core does with 2 of these blocks is 1 more
drawing:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 782px;&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;chfp&#x2F;drawings&#x2F;mx-block-matmul-light.svg?h=e5da03271f65d915d313&quot;
         alt=&quot;a row block of A drawn as an orange scale chip sA over 32 element cells, above a column block of B with scale chip sB; grey arrows pair the elements into a box reading 32 products, summed in float32; the two scale chips feed a box reading sA plus sB, exponents add, whose arrow joins the float32 partial sum, scaled by 1 shift&quot; width=&quot;782&quot; height=&quot;293&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;chfp&#x2F;drawings&#x2F;mx-block-matmul-dark.svg?h=45bf5d0d6754b6d112ba&quot;
         alt=&quot;a row block of A drawn as an orange scale chip sA over 32 element cells, above a column block of B with scale chip sB; grey arrows pair the elements into a box reading 32 products, summed in float32; the two scale chips feed a box reading sA plus sB, exponents add, whose arrow joins the float32 partial sum, scaled by 1 shift&quot; width=&quot;782&quot; height=&quot;293&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 43.&lt;&#x2F;span&gt; 2 MX blocks meet in the tensor core: the 32 element pairs multiply and sum in float32; the 2 scale bytes add as exponents and touch the sum once per block.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The block chassis also changed who does what inside FP8: with a
scale for every 32 values, a block rarely spans more than E4M3&#x27;s
18 doublings, so E4M3, with its 8 points per doubling against
E5M2&#x27;s 4, measured better for the gradients too, and the largest
published MXFP8 pretraining run (8 billion parameters, 15
trillion tokens) quantizes every tensor as E4M3 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-11&quot;&gt;11&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The 6-bit elements deserve their own cards, because they are the
family&#x27;s open seats: E2M3 keeps digits, E3M2 keeps reach, one
more run of the fork.&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 780px;&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;chfp&#x2F;drawings&#x2F;fp6-e2m3-card-light.svg?h=4783f7e843d173a50b9e&quot;
         alt=&quot;the bit fields of FP6 E2M3 drawn to scale: 1 sign bit, 2 exponent bits in orange, 3 mantissa bits, 6 bits per number; a card lists bias 1, ceiling 7.5, normal floor 1, ramp floor 0.125, step at 1.0 of 0.125 highlighted, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;780&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;fp6-e2m3-card-dark.svg?h=9e6184a9dea30d5914ca&quot;
         alt=&quot;the bit fields of FP6 E2M3 drawn to scale: 1 sign bit, 2 exponent bits in orange, 3 mantissa bits, 6 bits per number; a card lists bias 1, ceiling 7.5, normal floor 1, ramp floor 0.125, step at 1.0 of 0.125 highlighted, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;780&quot; height=&quot;342&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 44.&lt;&#x2F;span&gt; FP6 E2M3&#x27;s card: 3 mantissa bits, reach of only 5.9 doublings.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 673px;&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;chfp&#x2F;drawings&#x2F;fp6-e2m3-map-light.svg?h=8adfde58d5eb859b89cb&quot;
         alt=&quot;the whole FP6 E2M3 map drawn large: a dashed ramp interval and 3 intervals of 8 points each, boundaries at 1, 2 and 4, ceiling 7.5, every point of the orange interval from 1 to 2 labeled, a step bracket reading step 2 to the -3 equals 0.125, the ulp at 1.0, and the note no inf, no NaN, every code is a number&quot; width=&quot;673&quot; height=&quot;191&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;chfp&#x2F;drawings&#x2F;fp6-e2m3-map-dark.svg?h=18702864d324cf975997&quot;
         alt=&quot;the whole FP6 E2M3 map drawn large: a dashed ramp interval and 3 intervals of 8 points each, boundaries at 1, 2 and 4, ceiling 7.5, every point of the orange interval from 1 to 2 labeled, a step bracket reading step 2 to the -3 equals 0.125, the ulp at 1.0, and the note no inf, no NaN, every code is a number&quot; width=&quot;673&quot; height=&quot;191&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 45.&lt;&#x2F;span&gt; all of E2M3: 3 intervals of 8 points, 7.5 down to 0.125.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 775px;&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;chfp&#x2F;drawings&#x2F;fp6-e3m2-card-light.svg?h=f229d9ff47cff6a7460a&quot;
         alt=&quot;the bit fields of FP6 E3M2 drawn to scale: 1 sign bit, 3 exponent bits in orange, 2 mantissa bits, 6 bits per number; a card lists bias 3, ceiling 28 highlighted, normal floor 0.25, ramp floor 0.0625, step at 1.0 of 0.25, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;775&quot; height=&quot;342&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;chfp&#x2F;drawings&#x2F;fp6-e3m2-card-dark.svg?h=d88cb27dadc14ffeefed&quot;
         alt=&quot;the bit fields of FP6 E3M2 drawn to scale: 1 sign bit, 3 exponent bits in orange, 2 mantissa bits, 6 bits per number; a card lists bias 3, ceiling 28 highlighted, normal floor 0.25, ramp floor 0.0625, step at 1.0 of 0.25, and about 1 decimal digit; brackets tie each row to the field that sets it&quot; width=&quot;775&quot; height=&quot;342&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 46.&lt;&#x2F;span&gt; FP6 E3M2&#x27;s card: 1 more exponent bit, reach of 8.8 doublings.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 793px;&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;chfp&#x2F;drawings&#x2F;fp6-e3m2-map-light.svg?h=c15663050d297b18c3b5&quot;
         alt=&quot;the whole FP6 E3M2 map drawn large: a dashed ramp interval and 7 intervals of 4 points each, boundaries from 0.25 to 16, ceiling 28, every point of the orange interval from 1 to 2 labeled, a step bracket reading step 2 to the -2 equals 0.25, the ulp at 1.0, and the note no inf, no NaN, every code is a number&quot; width=&quot;793&quot; height=&quot;191&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;chfp&#x2F;drawings&#x2F;fp6-e3m2-map-dark.svg?h=430da77927a4590cc586&quot;
         alt=&quot;the whole FP6 E3M2 map drawn large: a dashed ramp interval and 7 intervals of 4 points each, boundaries from 0.25 to 16, ceiling 28, every point of the orange interval from 1 to 2 labeled, a step bracket reading step 2 to the -2 equals 0.25, the ulp at 1.0, and the note no inf, no NaN, every code is a number&quot; width=&quot;793&quot; height=&quot;191&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 47.&lt;&#x2F;span&gt; all of E3M2: 7 intervals of 4 points, 28 down to 0.0625.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Their maps are small enough to draw complete: 3 intervals of 8
points, 7 intervals of 4. Blackwell-class chips execute them at
full speed as MXFP6 &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-9&quot;&gt;9&lt;&#x2F;a&gt;][&lt;a href=&quot;#ref-24&quot;&gt;24&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; almost no published recipe uses
them yet. A format survives by having a job nobody else does.
FP8 is safer, FP4 is faster on paper, and FP6 is still looking
for its job.&lt;&#x2F;p&gt;
&lt;p&gt;NVIDIA&#x27;s NVFP4 variant tightens it further: 16-element blocks, a
fractional E4M3 scale instead of a power-of-two one, plus one
float32 scale per tensor, buying a finer fit for a quarter bit
more &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-10&quot;&gt;10&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The bytes carry the quarter bit, and the deeper
difference sits in the scale byte itself:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 735px;&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;chfp&#x2F;drawings&#x2F;nvfp4-block-anatomy-light.svg?h=81bccb19e58d2064adea&quot;
         alt=&quot;two byte lanes: MXFP4 as one E8M0 scale byte plus 16 element bytes, 17 bytes for 32 values, 4.25 bits each; NVFP4 as one E4M3 scale byte plus 8 element bytes and a dashed float32 tensor scale chip, 9 bytes for 16 values, 4.5 bits each plus about 0 amortized; below, the two scale bytes drawn bit by bit: E8M0 all exponent, powers of 2 only; E4M3 with 3 mantissa bits, so the scale can be 2.5, not only 2 or 4&quot; width=&quot;735&quot; height=&quot;322&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;chfp&#x2F;drawings&#x2F;nvfp4-block-anatomy-dark.svg?h=e5a82fb5bde544855390&quot;
         alt=&quot;two byte lanes: MXFP4 as one E8M0 scale byte plus 16 element bytes, 17 bytes for 32 values, 4.25 bits each; NVFP4 as one E4M3 scale byte plus 8 element bytes and a dashed float32 tensor scale chip, 9 bytes for 16 values, 4.5 bits each plus about 0 amortized; below, the two scale bytes drawn bit by bit: E8M0 all exponent, powers of 2 only; E4M3 with 3 mantissa bits, so the scale can be 2.5, not only 2 or 4&quot; width=&quot;735&quot; height=&quot;322&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 48.&lt;&#x2F;span&gt; the 2 block families as bytes. 4.25 against 4.5 bits per value, and the deeper difference: an E4M3 scale has mantissa bits, so it can be fractional.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Here is one NVFP4 block quantized by hand, every scale shown:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 758px;&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;chfp&#x2F;drawings&#x2F;nvfp4-two-level-light.svg?h=e4ba9c6ff94cdecc5dfd&quot;
         alt=&quot;3 lanes of intervals show 8 of an NVFP4 block&amp;#x27;s 16 values making the round trip; between the first 2 lanes the 2 scales are drawn: the tensor&amp;#x27;s float32 scale, 448 times 6 over 15.011 equals 179.07, divided by this block&amp;#x27;s E4M3 scale, 448, together times 0.3997; the values land on E2M1&amp;#x27;s 16 values, the 2 scales divide back out, and outcome chips read: 0.25 and 0.5 to 0, 3.2 17 percent off, 15.0 exact, it set the scales&quot; width=&quot;758&quot; height=&quot;365&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;chfp&#x2F;drawings&#x2F;nvfp4-two-level-dark.svg?h=a2a20a07f46e501c242e&quot;
         alt=&quot;3 lanes of intervals show 8 of an NVFP4 block&amp;#x27;s 16 values making the round trip; between the first 2 lanes the 2 scales are drawn: the tensor&amp;#x27;s float32 scale, 448 times 6 over 15.011 equals 179.07, divided by this block&amp;#x27;s E4M3 scale, 448, together times 0.3997; the values land on E2M1&amp;#x27;s 16 values, the 2 scales divide back out, and outcome chips read: 0.25 and 0.5 to 0, 3.2 17 percent off, 15.0 exact, it set the scales&quot; width=&quot;758&quot; height=&quot;365&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 49.&lt;&#x2F;span&gt; one NVFP4 block, every scale shown. the largest value sets the scales and survives exactly; the smallest fall to 0; the middle carries the percent-level error.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;The largest of the 16 values, 15.011, sets the scales and comes
back exactly; 0.25 and 0.5 fall below half of the smallest step
at that scale and come back as 0; 3.2002 comes back as 3.7528,
about 17% off (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p19-nvfp4-worked&quot;&gt;proof&lt;&#x2F;a&gt; runs every
number). A single 4-bit value is coarse. The bet of 4-bit
arithmetic is that billions of such errors, kept unbiased,
cancel on average.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p19-nvfp4-worked&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;p19_nvfp4_worked.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: one NVFP4 block, quantized by hand, every scale shown.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Re-derives the worked 1 x 16 example from NVIDIA&amp;#39;s recipe (as&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;walked through by Radical Numerics): a global FP32 scale brings&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 tensor&amp;#39;s largest value into range, each 16-value block gets an&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;FP8 E4M3 scale, and the elements land on the 16 values of E2M1.&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;E2M1_MAX&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; 6.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;E4M3_MAX&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; 448.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;BLOCK&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; 16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;E2M1_GRID&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;0.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.5&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.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; 1.5&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.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.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; 4.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; 6.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-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; rne_e2m1&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;x&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;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;Round each value to the nearest E2M1 value, ties to even.&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&gt;    grid&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;cat&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;([&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;E2M1_GRID&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&quot;&gt;E2M1_GRID&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;flip&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;    d&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;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;unsqueeze&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; grid&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;abs&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;    return&lt;&#x2F;span&gt;&lt;span&gt; grid&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;d&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;argmin&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt;dim&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-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;&#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;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.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.25&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.5&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.75356&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.251245&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.2002&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;                  4.5032&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; 15.011&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.012&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;0.312&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;5.50055&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.06&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;                  -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1.2526&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.025&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.5114&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; 7.0162&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;# level 1: one FP32 scale for the whole tensor&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;global_amax&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;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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;s_enc&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment z-punctuation&quot;&gt; = (&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;E4M3_MAX&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; E2M1_MAX&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; global_amax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;s_dec&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.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; s_enc&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;# level 2: one E4M3 scale for each block of 16&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;block_amax&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;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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;                    # one block here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dec_scale&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;block_amax&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&quot;&gt; E2M1_MAX&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; s_enc&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dec_scale_e4m3&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; dec_scale&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;to&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;float8_e4m3fn&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;enc_scale&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.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;dec_scale_e4m3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&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; s_dec&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;level 1, the tensor&amp;#39;s float32 scale: 448 x 6 &#x2F; &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;global_amax&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; = &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;s_enc&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.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;&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;level 2, this block&amp;#39;s E4M3 scale: &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;dec_scale_e4m3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&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;:g&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;together: &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;s_enc&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.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; &#x2F; &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;dec_scale_e4m3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&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;:g&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;enc_scale&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;, the factor every value is multiplied by&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;# quantize: scale, clamp to E2M1&amp;#39;s range, round onto its 16 values&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scaled&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;x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; enc_scale&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;clamp&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&quot;&gt;E2M1_MAX&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; E2M1_MAX&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;q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; rne_e2m1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;scaled&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;# dequantize: multiply back by both scales&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dq&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; dec_scale_e4m3&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&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; s_dec&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;global amax &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;global_amax&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;   block encode scale &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;enc_scale&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4f&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;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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;input&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-storage z-type&quot;&gt;:&amp;gt;9s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;scaled&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-storage z-type&quot;&gt;:&amp;gt;8s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;E2M1&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-storage z-type&quot;&gt;:&amp;gt;6s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;back&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-storage z-type&quot;&gt;:&amp;gt;9s&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-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&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;span&gt; c&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; d&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; zip&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; scaled&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; q&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dq&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;a&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;9.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;b&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;8.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;c&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;6.1f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;d&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;9.4f&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-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;read the two ends: 15.011 comes back as 15.011 exactly&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;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;(it set the scales), 0.25 and 0.5 come back as 0 (they&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;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;fell below half of E2M1&amp;#39;s smallest step at this scale),&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;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;3.2002 comes back as 3.7528: about 17&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;% o&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;ff. one 4-bit&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;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;number is coarse; the training recipe works because these&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;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;errors average out over billions of them.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p19_nvfp4_worked.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;One subtlety earned a hardware rule. The E8M0 scale can only be
a power of 2, so a block whose largest value is 5.9 must round
its scale one way or the other, and the 2 ways are not close:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing&quot; style=&quot;--dw: 668px;&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;chfp&#x2F;drawings&#x2F;scale-rounding-light.svg?h=d63d2dc393ef3d4933cf&quot;
         alt=&quot;a block of 6 intervals all holding 5.9; below, the same E2M1 points drawn under the 2 candidate scales: with the scale rounded down the points reach only 3, and 5.9&amp;#x27;s arrow clamps back to the last point, 49 percent off on every value; with the scale rounded up the points reach 6, and 5.9 lands beside 6.00, 1.7 percent off&quot; width=&quot;668&quot; height=&quot;363&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;chfp&#x2F;drawings&#x2F;scale-rounding-dark.svg?h=94cdc0d49f67a9f38fa0&quot;
         alt=&quot;a block of 6 intervals all holding 5.9; below, the same E2M1 points drawn under the 2 candidate scales: with the scale rounded down the points reach only 3, and 5.9&amp;#x27;s arrow clamps back to the last point, 49 percent off on every value; with the scale rounded up the points reach 6, and 5.9 lands beside 6.00, 1.7 percent off&quot; width=&quot;668&quot; height=&quot;363&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 50.&lt;&#x2F;span&gt; the scale-rounding rule: round the block scale up. the first spec said down, and the difference is a failed training run.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;NVIDIA measured the difference as the gap between divergence and
parity at trillion-token scale &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-11&quot;&gt;11&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; &lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p11-mxblock&quot;&gt;proof&lt;&#x2F;a&gt;
shows the mechanism in 2 lines.&lt;&#x2F;p&gt;
&lt;p&gt;The whole scaling story now fits on one ladder, and every recipe
of the last 5 sections is 1 rung of it:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 746px;&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;chfp&#x2F;drawings&#x2F;granularity-ladder-light.svg?h=fab1e883f7fe41f0f748&quot;
         alt=&quot;5 rungs from coarse to fine, each with the same 4096 by 4096 weight drawn and its scale chips tied to what they serve by brackets and dashed lines: loss scaling with 1 scale bracketed over the whole backward pass, per tensor for the Hopper FP8 recipes, per 128 by 128 tile for DeepSeek-V3 with 1,024 scales, per block of 32 for the MX formats with 524,288 scales, and per 16 plus 1 global for NVFP4 with 1,048,577 scales; a right column counts the extra bits per value, 0 up to 0.5&quot; width=&quot;746&quot; height=&quot;565&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;chfp&#x2F;drawings&#x2F;granularity-ladder-dark.svg?h=e5e83800d9eff6ed3f93&quot;
         alt=&quot;5 rungs from coarse to fine, each with the same 4096 by 4096 weight drawn and its scale chips tied to what they serve by brackets and dashed lines: loss scaling with 1 scale bracketed over the whole backward pass, per tensor for the Hopper FP8 recipes, per 128 by 128 tile for DeepSeek-V3 with 1,024 scales, per block of 32 for the MX formats with 524,288 scales, and per 16 plus 1 global for NVFP4 with 1,048,577 scales; a right column counts the extra bits per value, 0 up to 0.5&quot; width=&quot;746&quot; height=&quot;565&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 51.&lt;&#x2F;span&gt; the granularity ladder. down it, an outlier damages less; up it, fewer bytes go to scales. every scaling recipe on this page is 1 rung.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;h2 id=&quot;training-at-4-bits&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#training-at-4-bits&quot; aria-label=&quot;Anchor link for: training-at-4-bits&quot;&gt;Training at 4 bits&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;The destination format, E2M1, was drawn whole 2 sections back:
15 values, 12x wide. Training inside those 16 codes
needs 3 rescues beyond blocks,
and each is an idea from earlier on this page pushed one step:&lt;&#x2F;p&gt;
&lt;p&gt;The first is stochastic rounding, the lost-update repair now
doing the main work. At 4 bits nearly every update is below half the
local step, so deterministic rounding freezes everything; rounding
up with probability proportional to progress is unbiased in
expectation. Our proof runs 10,000 micro-updates: nearest
rounds to a frozen 1.0, stochastic reaches a mean of 3.0113
against a true 3.0, noisy per run and honest on average
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p12-sr-drift&quot;&gt;proof&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p12-sr-drift&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;p12_sr_drift.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: round-to-nearest is biased when every step is smaller than&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;half the local spacing; stochastic rounding is unbiased 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;expectation. The same fact that freezes bf16 weights, isolated.&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;span&gt;N&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; u&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; TRIALS&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; 10000&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; 2e-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; 50&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;   # each step ~1&#x2F;39 of bf16&amp;#39;s step at 1.0&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; sr_walk&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;seed&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;    g&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;Generator&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&gt;seed&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;    val&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.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; _&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;N&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;        x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; val&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; u&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        lo&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&gt;x&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;bfloat16&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;        lo_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; lo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float&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;        hi_f&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;nextafter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lo&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;tensor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;float&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;inf&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-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;bfloat16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span&gt;float&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-keyword&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; lo_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            hi_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; lo_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; lo_f&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;nextafter&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;lo&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;tensor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span class=&quot;z-support&quot;&gt;float&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;-inf&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-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;bfloat16&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span&gt;float&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;        p&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;span class=&quot;z-keyword&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; hi_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; lo_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; lo_f&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-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;hi_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; lo_f&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;        val&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; hi_f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; if&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;rand&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;((),&lt;&#x2F;span&gt;&lt;span class=&quot;z-variable&quot;&gt; generator&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;g&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;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; p&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&lt;&#x2F;span&gt;&lt;span&gt; lo_f&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&gt; val&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;rne&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;1.0&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;bfloat16&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;N&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;    rne&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; rne&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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&gt;u&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;bfloat16&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;finals&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&gt;sr_walk&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;s&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; s&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&quot;&gt;TRIALS&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;true&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.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; N&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; u&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;true value                     : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;true&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.4f&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;round-to-nearest, &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-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; steps  : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;rne&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;  (frozen)&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;stochastic rounding, &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{TRIALS}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; runs  : &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;mean &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;finals&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mean&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;, spread +-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;finals&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;std&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;span class=&quot;z-storage z-type&quot;&gt;:.4f&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-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;unbiased in expectation, noisy per run: that is the trade.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p12_sr_drift.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;p&gt;The second is the random Hadamard transform: rotate the tensor by
an orthogonal matrix before quantizing and undo it after, which
changes no matmul (the rotation cancels) but spreads an outlier&#x27;s
energy across every coordinate:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 754px;&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;chfp&#x2F;drawings&#x2F;rht-before-after-light.svg?h=645262d89664055ead8f&quot;
         alt=&quot;2 histograms: before, a small bell with 1 dominating orange spike, max over mean 11.8; an arrow labeled rotate by H carrying a small 2 by 2 plus and minus matrix; after, the same energy spread into a wider bell with no spike, max over mean 2.6; max and mean are drawn as level marks on both panels&quot; width=&quot;754&quot; height=&quot;221&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;chfp&#x2F;drawings&#x2F;rht-before-after-dark.svg?h=ec5493228bb73a4d0ad9&quot;
         alt=&quot;2 histograms: before, a small bell with 1 dominating orange spike, max over mean 11.8; an arrow labeled rotate by H carrying a small 2 by 2 plus and minus matrix; after, the same energy spread into a wider bell with no spike, max over mean 2.6; max and mean are drawn as level marks on both panels&quot; width=&quot;754&quot; height=&quot;221&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 52.&lt;&#x2F;span&gt; the rotation rescue, measured: max&#x2F;mean falls from 11.8 to 2.6 and nothing about the mathematics moved.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Our 64-value proof takes the max-to-mean ratio from 11.8 to 2.6
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p13-hadamard&quot;&gt;proof&lt;&#x2F;a&gt;). The third is selective precision:
the first and last layers, where quantization error is measured
to hurt most, stay in higher precision. With all three plus
NVFP4, a 12-billion-parameter model has been pretrained on 10
trillion tokens at nearly the same loss as its FP8 twin
&lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-10&quot;&gt;10&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; MXFP4 needs more tokens for the same loss
&lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-12&quot;&gt;12&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. Where each rescue is applied is itself measured
engineering &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-10&quot;&gt;10&lt;&#x2F;a&gt;][&lt;a href=&quot;#ref-24&quot;&gt;24&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;: stochastic rounding only on the gradients
(on the forward pass it adds noise for nothing, so weights and
activations round to nearest); the Hadamard rotation only on
the inputs of the weight-gradient matmul; and weights are
scaled in 16 x 16 squares rather than 1 x 16 rows, for a reason
worth drawing:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 904px;&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;chfp&#x2F;drawings&#x2F;transpose-blocks-light.svg?h=9f953348294b2f5196a3&quot;
         alt=&quot;the same 8 by 8 weight matrix drawn 4 times: blocked along rows for the forward pass; blocked along columns for the transposed backward pass; a clash panel where one red-framed interval sits inside both a row block and a column block, their 2 scale chips pointing at it, 1 weight, 2 blocks, 2 different scales; and 16 by 16 squares, the same both ways&quot; width=&quot;904&quot; height=&quot;266&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;chfp&#x2F;drawings&#x2F;transpose-blocks-dark.svg?h=8deedf81284ef6253fb0&quot;
         alt=&quot;the same 8 by 8 weight matrix drawn 4 times: blocked along rows for the forward pass; blocked along columns for the transposed backward pass; a clash panel where one red-framed interval sits inside both a row block and a column block, their 2 scale chips pointing at it, 1 weight, 2 blocks, 2 different scales; and 16 by 16 squares, the same both ways&quot; width=&quot;904&quot; height=&quot;266&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 53.&lt;&#x2F;span&gt; the transpose problem. blocks run along the reduction direction, so W and W-transposed disagree; squares are the truce, paid only for the weights.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;A row-scaled weight matrix and its column-scaled transpose are 2
different quantized matrices, and the forward and backward
passes must see the same weights. The last roughly 15% of
layers stay in higher precision, and if a gap to the wide
baseline remains at the end, switching the forward pass to
higher precision for the final phase of training closes most
of it &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-10&quot;&gt;10&lt;&#x2F;a&gt;][&lt;a href=&quot;#ref-24&quot;&gt;24&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. 4-bit training is real and it is young; treat
recipes as versioned, not settled.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p13-hadamard&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;p13_hadamard.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: an orthogonal rotation spreads one outlier&amp;#39;s energy across&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;every coordinate, without changing the vector&amp;#39;s length or the matmul&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;it feeds (H is orthogonal). The max shrinks; the format&amp;#39;s points get&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;used again.&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-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; hadamard&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;n&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;    H&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;1.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 class=&quot;z-keyword&quot;&gt;    while&lt;&#x2F;span&gt;&lt;span&gt; H&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;shape&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 z-operator&quot;&gt; &amp;lt;&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        H&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;cat&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;cat&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;([&lt;&#x2F;span&gt;&lt;span&gt;H&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; H&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;cat&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;([&lt;&#x2F;span&gt;&lt;span&gt;H&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;H&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; 0&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-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-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0.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-keyword&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; H&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;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&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; 64&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;randn&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-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 0.5&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-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;7&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 class=&quot;z-constant z-numeric&quot;&gt;23&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 class=&quot;z-constant z-numeric&quot;&gt;51&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; 8.0&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;7.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; 6.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;H&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; hadamard&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;&#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; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; @&lt;&#x2F;span&gt;&lt;span&gt; H&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;norm preserved: &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&gt;allclose&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;norm&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;norm&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;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; name&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&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;before&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 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 H&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; y&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;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:8s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; max &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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;:.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;   mean &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;mean&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;:.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;   &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;max&#x2F;mean &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;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;mean&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;span class=&quot;z-storage z-type&quot;&gt;:.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;&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;chfp&#x2F;proofs&#x2F;p13_hadamard.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;the-other-grids&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-other-grids&quot; aria-label=&quot;Anchor link for: the-other-grids&quot;&gt;The other grids&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Inference relaxed one constraint: if only the weights are
quantized and arithmetic runs wider, the 16 points need not
be a float at all. 3 designs compete on the same bell-shaped distribution of
trained weights:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 838px;&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;chfp&#x2F;drawings&#x2F;three-grids-light.svg?h=da30a612d72e45ef0bcb&quot;
         alt=&quot;a bell of trained weights above 3 aligned rows of 16 tick marks: INT4 equal spacing, FP4 crowded at zero, NF4 matched to the bell; a warm band marks within 0.25 of zero, holding 3, 5 and 6 points per row; at the right each row&amp;#x27;s measured mean error is drawn as a bar: 0.163, 0.106, 0.106, with NF4 the best tail&quot; width=&quot;838&quot; height=&quot;341&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;chfp&#x2F;drawings&#x2F;three-grids-dark.svg?h=d39e07fff6b73b3feded&quot;
         alt=&quot;a bell of trained weights above 3 aligned rows of 16 tick marks: INT4 equal spacing, FP4 crowded at zero, NF4 matched to the bell; a warm band marks within 0.25 of zero, holding 3, 5 and 6 points per row; at the right each row&amp;#x27;s measured mean error is drawn as a bar: 0.163, 0.106, 0.106, with NF4 the best tail&quot; width=&quot;838&quot; height=&quot;341&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 54.&lt;&#x2F;span&gt; 16 points, 3 designs, one weight distribution, measured. equal spacing wastes the tails; the float grid crowds zero; the quantile grid follows the weights.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;INT4 spaces its points equally and wastes them where weights
are rare. FP4 doubles its steps and crowds zero. NF4, from the QLoRA work,
places its 16 values at the quantiles of a gaussian, which is
approximately what trained weights are, and wins the tail
(&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p14-grids&quot;&gt;proof&lt;&#x2F;a&gt;, &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-13&quot;&gt;13&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;). Around these grids
grew the post-training toolkit: GPTQ chooses each weight&#x27;s
rounding to compensate the error of the previous ones
&lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-14&quot;&gt;14&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; AWQ rescales channels so the few weights that
matter most land on finer points &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-15&quot;&gt;15&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; and the same
how-many-bits question now runs through the KV cache and even optimizer
states, where 8-bit Adam keeps its moments in blocks with
per-block scales &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-16&quot;&gt;16&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;, the MX idea arriving from the
other direction.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p14-grids&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;p14_grids.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: three ways to spend sixteen points. INT4&amp;#39;s uniform grid,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;FP4 E2M1&amp;#39;s doubling grid, and NF4&amp;#39;s quantile grid (the sixteen&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;values from the QLoRA paper), each quantizing the same gaussian&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;weights with one absmax scale.&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;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;w&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;100_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-constant&quot;&gt;INT4&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;linspace&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;7&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; 7&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; 15&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; 7.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-definition z-comment z-comment&quot;&gt;            # symmetric int4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;E2M1&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;0.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.5&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.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; 1.5&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.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.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; 4.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; 6.0&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; 6.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-constant&quot;&gt;E2M1&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;cat&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&quot;&gt;E2M1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;flip&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&quot;&gt; E2M1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;]).&lt;&#x2F;span&gt;&lt;span&gt;unique&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;NF4&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-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;1.0&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;0.6961928009986877&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;0.5250730514526367&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;    -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0.39491748809814453&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;0.28444138169288635&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;0.18477343022823334&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;    -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;0.09105003625154495&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.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.07958029955625534&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.16093020141124725&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.24611230194568634&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.33791524171829224&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.44070982933044434&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.5626170039176941&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.7229568362236023&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.0&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;  # Dettmers et al. 2023&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;s&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; w&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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; name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; grid&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;INT4 uniform&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&quot;&gt; INT4&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;FP4 E2M1&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&quot;&gt; E2M1&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;NF4 quantile&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&quot;&gt; NF4&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;    q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; grid&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[( (&lt;&#x2F;span&gt;&lt;span&gt;w&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; s&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;unsqueeze&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; grid&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; ).&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;argmin&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-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; s&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    err&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;q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; w&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;abs&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;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:14s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt; mean abs err &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;err&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mean&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;:.4f&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;p99 &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;err&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;quantile&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.99&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;:.4f&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-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;the bell wants points where the bell is; NF4 puts them there.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p14_grids.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
&lt;h2 id=&quot;the-whole-family-on-one-sheet&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-whole-family-on-one-sheet&quot; aria-label=&quot;Anchor link for: the-whole-family-on-one-sheet&quot;&gt;The whole family on one sheet&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Every format on this page has now been met. Gather them:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 1178px;&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;chfp&#x2F;drawings&#x2F;family-sheet-light.svg?h=b258e6e64f0f2dee5473&quot;
         alt=&quot;every format on one sheet, one row each for float64, float32, TF32, bfloat16, float16, FP8 E5M2, FP8 E4M3, FP6 E3M2, FP6 E2M3 and FP4 E2M1: the bit fields drawn to one shared scale with exponents orange and mantissas blue, the reach drawn as orange bars in doublings with broken bars for 2098, 277, 264 and 261, and digits as blue bars from 16.0 down to 0.6; the TF32 row is dashed, a read mode; below, the block formats join: MXFP8, MXFP6, MXFP4 and NVFP4, each drawn as its shared scale byte plus its element bits, 8.25 down to 4.25 and 4.5 bits per value, their reach an element bar with dashed arrows, slides per block&quot; width=&quot;1178&quot; height=&quot;917&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;chfp&#x2F;drawings&#x2F;family-sheet-dark.svg?h=d9b1f791d7d81aec2e3c&quot;
         alt=&quot;every format on one sheet, one row each for float64, float32, TF32, bfloat16, float16, FP8 E5M2, FP8 E4M3, FP6 E3M2, FP6 E2M3 and FP4 E2M1: the bit fields drawn to one shared scale with exponents orange and mantissas blue, the reach drawn as orange bars in doublings with broken bars for 2098, 277, 264 and 261, and digits as blue bars from 16.0 down to 0.6; the TF32 row is dashed, a read mode; below, the block formats join: MXFP8, MXFP6, MXFP4 and NVFP4, each drawn as its shared scale byte plus its element bits, 8.25 down to 4.25 and 4.5 bits per value, their reach an element bar with dashed arrows, slides per block&quot; width=&quot;1178&quot; height=&quot;917&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 55.&lt;&#x2F;span&gt; the whole family on one sheet, block formats included: orange buys reach, blue buys digits, and a shared scale slides a narrow row anywhere. every scalar number derived from the bit counts and checked against torch where torch carries the format (proof below).&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p18-family-sheet&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;p18_family_sheet.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 whole-family sheet, derived from each format&amp;#39;s bit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;counts and checked against torch wherever torch has the type.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;For every format: ceiling, smallest positive value, the number of&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;doublings between them (log2 of their ratio), step at 1.0, and&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;decimal digits. kind says how the top exponent codes are used:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  ieee  top exponent code reserved for inf and NaN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  fn    no inf; only the top mantissa pattern of the top code is NaN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;  all   no inf and no NaN; every code is a number (FP4 E2M1)&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; math&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; family&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;E&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; M&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; bias&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; kind&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; kind&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;ieee&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;        emax&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; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; E&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;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ceiling&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment 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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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 z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; emax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    elif&lt;&#x2F;span&gt;&lt;span&gt; kind&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;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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        emax&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; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; E&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ceiling&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment 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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&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-keyword z-operator&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 z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; emax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-keyword&quot;&gt;    else&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;  # all&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        emax&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; 2&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; E&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ceiling&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment 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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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 z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; emax&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    smallest&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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-comment z-comment&quot;&gt;          # smallest subnormal&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;&#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;ceiling&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; ceiling&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;smallest&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; smallest&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;doublings&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; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;ceiling&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; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;smallest&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;step1&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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;&#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;digits&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;M&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; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log10&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&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-constant&quot;&gt;ROWS&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-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;float64&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; 11&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; 52&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; 1023&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;ieee&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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float64&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;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 class=&quot;z-constant z-numeric&quot;&gt; 8&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; 23&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; 127&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;ieee&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; 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 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;TF32&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; 8&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;span class=&quot;z-constant z-numeric&quot;&gt; 127&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;ieee&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-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 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;bfloat16&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; 8&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; 7&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; 127&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;ieee&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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;bfloat16&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;float16&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; 5&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;span class=&quot;z-constant z-numeric&quot;&gt; 15&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;ieee&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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float16&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;FP8 E5M2&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; 5&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; 15&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;ieee&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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float8_e5m2&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;FP8 E4M3&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; 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; 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; 7&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;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; torch&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;float8_e4m3fn&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;FP6 E3M2&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; 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; 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;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;all&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-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 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;FP6 E2M3&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; 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;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-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;all&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-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 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;FP4 E2M1&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; 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; 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-punctuation z-definition z-string&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;all&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-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 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;&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;format&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-storage z-type&quot;&gt;:10s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;ceiling&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-storage z-type&quot;&gt;:&amp;gt;10s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;smallest&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-storage z-type&quot;&gt;:&amp;gt;10s&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 class=&quot;z-punctuation z-definition z-string&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;doublings&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-storage z-type&quot;&gt;:&amp;gt;9s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;step at 1&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-storage z-type&quot;&gt;:&amp;gt;10s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&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;digits&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-storage z-type&quot;&gt;:&amp;gt;6s&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-keyword&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; name&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; M&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; kind&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; dt&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; in&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; ROWS&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;    f&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; family&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; M&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; bias&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; kind&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;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:10s&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;f&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;ceiling&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;10.4g&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;f&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;smallest&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;10.3g&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;f&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;doublings&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;9.1f&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;f&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;step1&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;10.4g&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;f&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;digits&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;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;&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;    if&lt;&#x2F;span&gt;&lt;span&gt; dt&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;        fi&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;finfo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;dt&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;        assert&lt;&#x2F;span&gt;&lt;span&gt; f&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;ceiling&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-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;, (&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; f&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;ceiling&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; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max&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;        assert&lt;&#x2F;span&gt;&lt;span&gt; f&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;step1&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-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eps&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;, (&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; f&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;step1&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; fi&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;eps&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;torch.finfo agrees on ceiling and step for every format &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 class=&quot;z-punctuation z-definition z-string&quot;&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;torch carries&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;(float64&#x2F;32&#x2F;16, bfloat16, both FP8 types).&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;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;FP6 sheets from the same formulas; Blackwell tensor cores &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 class=&quot;z-punctuation z-definition z-string&quot;&gt;      &amp;quot;&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;run them,&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;torch has no storage type for them yet.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p18_family_sheet.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 sheet by columns. The bit bars share one scale: you
can see float64 dwarf everything and the narrow rows shrink to
almost nothing. The reach bars collapse from float64&#x27;s 2,098
doublings to E2M1&#x27;s 3.6; every format from float16 down fits
its whole reach on the drawing. The digits column falls from 16
to 0.6. The block rows at the bottom are the repair: the same
narrow elements, plus a shared scale whose dashed arrows say
what it does, sliding each block&#x27;s small reach to wherever its
values live. And the sheet is the page in one drawing:
everything before the field guide taught you what the columns
mean, and everything after it was the story of how the narrow
rows are made usable.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;where-the-bytes-live&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#where-the-bytes-live&quot; aria-label=&quot;Anchor link for: where-the-bytes-live&quot;&gt;Where the bytes live&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;All of it, on one shelf:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 857px;&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;chfp&#x2F;drawings&#x2F;bytes-ladder-light.svg?h=fe7973b12c09c2d9a94f&quot;
         alt=&quot;4 bars for the weights of a 7,000,000,000-weight model: float32 28 GB, bfloat16 14, FP8 with scales 7.1, MXFP4 with scales 3.7; one stacked bar for what a bf16 mixed-precision training run holds: weights 14, gradients 14, master copy 28, Adam m 28, Adam v 28, together 112 GB, 16 bytes per weight; and a serving panel for a 70B-class chat model with 80 layers and 8 KV heads of 128 dims at 131,072 tokens: weights W4 37 GB plus float16 KV cache 43 GB exactly fills a dashed one-80-GB-GPU line, while FP8 KV at 21.5 GB leaves half the card free; every number derived in the drawing&quot; width=&quot;857&quot; height=&quot;696&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;chfp&#x2F;drawings&#x2F;bytes-ladder-dark.svg?h=691368c165c626125d23&quot;
         alt=&quot;4 bars for the weights of a 7,000,000,000-weight model: float32 28 GB, bfloat16 14, FP8 with scales 7.1, MXFP4 with scales 3.7; one stacked bar for what a bf16 mixed-precision training run holds: weights 14, gradients 14, master copy 28, Adam m 28, Adam v 28, together 112 GB, 16 bytes per weight; and a serving panel for a 70B-class chat model with 80 layers and 8 KV heads of 128 dims at 131,072 tokens: weights W4 37 GB plus float16 KV cache 43 GB exactly fills a dashed one-80-GB-GPU line, while FP8 KV at 21.5 GB leaves half the card free; every number derived in the drawing&quot; width=&quot;857&quot; height=&quot;696&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 56.&lt;&#x2F;span&gt; 7 billion weights 4 ways, the 112 GB training footprint, and the serving arithmetic: with float16 KV one conversation fills an 80 GB GPU; FP8 KV halves it. the bus carries all of it, every step and every token.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;These panels are the honest reason this whole family of
formats exists. A
7-billion-parameter model is 28 GB of float32 or 3.7 GB of MXFP4
with its scales; a training run holds 8x its bf16 weights; and
in serving, the KV cache decides how many conversations one GPU
carries. gpt-oss ships its 120-billion-parameter
mixture-of-experts in MXFP4 so it fits a single 80 GB GPU
&lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-9&quot;&gt;9&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Serving adds a loop of its own. A chat model generates 1 token
at a time, and every token&#x27;s step reads all the weights and the
whole KV cache, the stored attention keys and values of every
token so far; serving engines keep that cache in fixed-size
pages so many conversations can grow side by side without
fragmenting memory &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-29&quot;&gt;29&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. That loop, end to end:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 786px;&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;chfp&#x2F;drawings&#x2F;inference-flow-light.svg?h=eb77d360a5dd8e2ef325&quot;
         alt=&quot;one decode step of a serving engine: a dashed GPU memory region holds all the weights, W4 plus scales, and the KV cache in fixed-size pages drawn as a grid of blue blocks, 1 new entry per token; on the right, the compute loop: attention reads the whole cache so far, MLP matmuls read all the weights, logits in float32, sample 1 token; a black arrow streams all 37 GB of weights and a blue arrow streams the whole cache into the loop on every token, a dashed blue arrow writes 1 new KV entry back, and an orange arrow carries the new token back to the top&quot; width=&quot;786&quot; height=&quot;430&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;chfp&#x2F;drawings&#x2F;inference-flow-dark.svg?h=70548908da8e1f780ecc&quot;
         alt=&quot;one decode step of a serving engine: a dashed GPU memory region holds all the weights, W4 plus scales, and the KV cache in fixed-size pages drawn as a grid of blue blocks, 1 new entry per token; on the right, the compute loop: attention reads the whole cache so far, MLP matmuls read all the weights, logits in float32, sample 1 token; a black arrow streams all 37 GB of weights and a blue arrow streams the whole cache into the loop on every token, a dashed blue arrow writes 1 new KV entry back, and an orange arrow carries the new token back to the top&quot; width=&quot;786&quot; height=&quot;430&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 57.&lt;&#x2F;span&gt; one generated token, end to end. every token streams the full weights and the full cache across the memory bus; halving the bytes halves the time per token [29].&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;h2 id=&quot;hardware-truth&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#hardware-truth&quot; aria-label=&quot;Anchor link for: hardware-truth&quot;&gt;Hardware truth&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Throughput follows the same halvings on hardware that speaks
the format natively, and only there: on a GPU that must
dequantize in software, a narrow format saves memory but not
time. That warning is measurable.
One published comparison ran the same image-generation model on
a GPU with no MXFP8 hardware: the MXFP8 file was smaller, and
generation was slower than bfloat16 (112.8 seconds against
96.0), because every block had to be unpacked in software &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-25&quot;&gt;25&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.
The same tests ranked output quality FP32, then FP16 and BF16
about equal, then MXFP8, then per-tensor-scaled FP8, then
NVFP4, then plain FP8 last &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-25&quot;&gt;25&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;: size and quality move
together, and a scale at any granularity beats no scale. Who
speaks what natively today: NVIDIA&#x27;s Blackwell chips run
MXFP8&#x2F;6&#x2F;4 and NVFP4 in their tensor cores &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-24&quot;&gt;24&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; AMD&#x27;s newest
Instinct chips run the MX family &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-26&quot;&gt;26&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; Hopper GPUs and
Trainium2 can store MX blocks but unpack them for the math &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-26&quot;&gt;26&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.
The same facts, drawn so a spec sheet can be checked against
them:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 840px;&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;chfp&#x2F;drawings&#x2F;hardware-matrix-light.svg?h=e9632882c3980c7f48ab&quot;
         alt=&quot;a grid of chips against formats: rows NVIDIA H100, NVIDIA B200, AMD MI355X, Google TPU, AWS Trainium2 and Apple M3; columns float64, TF32, 16-bit, FP8, MX 8 6 4 and NVFP4; filled orange squares mark native tensor-core support, flat grey half-squares mark storage that is unpacked in software; B200 fills every column, H100 and Trainium2 carry half-squares under MX&quot; width=&quot;840&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;chfp&#x2F;drawings&#x2F;hardware-matrix-dark.svg?h=424075d367ce81f6391c&quot;
         alt=&quot;a grid of chips against formats: rows NVIDIA H100, NVIDIA B200, AMD MI355X, Google TPU, AWS Trainium2 and Apple M3; columns float64, TF32, 16-bit, FP8, MX 8 6 4 and NVFP4; filled orange squares mark native tensor-core support, flat grey half-squares mark storage that is unpacked in software; B200 fills every column, H100 and Trainium2 carry half-squares under MX&quot; width=&quot;840&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 58.&lt;&#x2F;span&gt; who speaks what, from this page&#x27;s sources [20][24][25][26]. filled: runs it in the tensor cores. half: stores it, unpacks in software. empty: these sources make no claim.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;And what a native format buys, next to the measured price of one
the hardware cannot speak:&lt;&#x2F;p&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 974px;&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;chfp&#x2F;drawings&#x2F;throughput-ladder-light.svg?h=41fc86c5d16c835147a7&quot;
         alt=&quot;horizontal bars of dense matmul rate in multiples of bfloat16: float64 a sliver, far below; TF32 at 0.5x; bfloat16 at 1x; FP8 at 2x, half the bits, twice the rate; FP4 at 4x on Blackwell; below, a red outlined bar at 0.85x for MXFP8 on a GPU with no MX hardware, measured 112.8 seconds against 96.0, slower than bfloat16 because every block is unpacked in software&quot; width=&quot;974&quot; height=&quot;420&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;chfp&#x2F;drawings&#x2F;throughput-ladder-dark.svg?h=63c965a5ab88f13c07dc&quot;
         alt=&quot;horizontal bars of dense matmul rate in multiples of bfloat16: float64 a sliver, far below; TF32 at 0.5x; bfloat16 at 1x; FP8 at 2x, half the bits, twice the rate; FP4 at 4x on Blackwell; below, a red outlined bar at 0.85x for MXFP8 on a GPU with no MX hardware, measured 112.8 seconds against 96.0, slower than bfloat16 because every block is unpacked in software&quot; width=&quot;974&quot; height=&quot;420&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 59.&lt;&#x2F;span&gt; each native halving doubles the matmul rate [19][24]; the bottom bar is the measured cost when the hardware cannot speak the format [25].&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Read a spec sheet&#x27;s format row before believing its speedup
column.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-contract&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-contract&quot; aria-label=&quot;Anchor link for: the-contract&quot;&gt;The contract&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Two runs of the same training script, same seeds, same data, on
two different GPUs, will not match bit for bit, and now every
reason is on this page: sums land in different orders across
different core counts; tensor cores round differently than the
unfused loop; TF32 trims mantissas silently on one machine and
not another. And there is a quieter actor: the compiler. IEEE
754 fixes each instruction&#x27;s bits, but no language promises
which instructions your line becomes. A compiler may legally
fuse a * b + c into 1 fused multiply-add (1 rounding) or keep it
as 2 instructions (2 roundings), and the choice can flip with an
optimization flag or with something as small as inlining a
function &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-27&quot;&gt;27&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;; the rounding section&#x27;s proof showed the 2 paths
disagreeing. torch.compile is a compiler too: its fused kernels
are allowed to round differently than eager mode &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-28&quot;&gt;28&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. And
rounding in 2 hops is not rounding once: take an exact value to
float64 first and then to float32, and you can land on a
different number than going straight to float32, because the
first rounding can create a tie the second one breaks the other
way (&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p23-double-rounding&quot;&gt;proof&lt;&#x2F;a&gt; builds one). This
double rounding is why the old x87 unit, which computed in
80-bit registers and rounded again on every store to memory,
made a program&#x27;s bits depend on register allocation &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-4&quot;&gt;4&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. None
of it is a bug; all of it is the map.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p23-double-rounding&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;p23_double_rounding.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: rounding twice can differ from rounding once. Take the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;exact value 1 + 2^-24 + 2^-60. Rounded straight to float32 it goes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;UP to 1 + 2^-23, because it sits just above the halfway point.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;Rounded first to float64, the tiny 2^-60 tail is lost and the&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;float64 result lands exactly ON the halfway point; rounding that&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;to float32 then goes DOWN to 1.0 by ties-to-even. Two legal&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;paths, 2 different answers: double rounding, the failure mode of&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;every system that computes wider first and stores narrower.&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;from&lt;&#x2F;span&gt;&lt;span&gt; fractions&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Fraction&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; F&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; numpy&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; np&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;exact&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; F&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-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; F&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; 2&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; 24&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; F&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; 2&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; 60&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;# path 1: round once, straight to float32 (decided exactly)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lo&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; hi&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; F&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; F&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-keyword z-operator&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; F&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; 2&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; 23&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;          # float32 neighbors&lt;&#x2F;span&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; lo&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; exact&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; hi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;once&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; np&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;span class=&quot;z-constant z-numeric&quot;&gt;1.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 class=&quot;z-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;exact&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; lo&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; &amp;lt;=&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt; (&lt;&#x2F;span&gt;&lt;span&gt;hi&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; exact&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt; else&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;       np&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;span class=&quot;z-constant z-numeric&quot;&gt;1.0&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.0&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;23&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;# path 2: round to float64 first, then to float32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;via64&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; float&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;exact&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;                          # correctly rounded&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;twice&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; np&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;span&gt;via64&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;exact value          : 1 + 2^-24 + 2^-60&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;rounded once to fp32 : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;once&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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;fp64 first           : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;via64&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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;then to fp32         : &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;twice&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;!r&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-keyword&quot;&gt;assert&lt;&#x2F;span&gt;&lt;span&gt; once&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; np&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;span class=&quot;z-constant z-numeric&quot;&gt;1.0&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.0&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;23&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;assert&lt;&#x2F;span&gt;&lt;span&gt; twice&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; np&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;span class=&quot;z-constant z-numeric&quot;&gt;1.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 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;the 2 paths disagree. the old x87 unit computed in 80-bit&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;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;registers and rounded again on every spill to memory, so a&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;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;program&amp;#39;s bits depended on register allocation; SSE and its&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;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;one-width successors retired the problem, and this proof is&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;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;why narrow-then-narrower casts still deserve suspicion.&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;&#x2F;code&gt;&lt;&#x2F;pre&gt;
    &lt;a class=&quot;proof-raw&quot; href=&quot;&#x2F;ldr&#x2F;chfp&#x2F;proofs&#x2F;p23_double_rounding.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&gt;
 The
working rules: compare models with tolerances
(&lt;code&gt;torch.testing.assert_close&lt;&#x2F;code&gt;, never &lt;code&gt;==&lt;&#x2F;code&gt;); flip
&lt;code&gt;torch.use_deterministic_algorithms(True)&lt;&#x2F;code&gt; when you need
repeatability inside one machine and accept the speed cost; and
when a loss goes nan, hunt the overflow upstream (an unmasked
softmax, an unscaled fp16 gradient, a variance of zero) rather
than rerolling the seed. The nan at the top of this page has the same kind of cause as
the 0.1 + 0.2: some number moved past the edge of its map.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;where-this-came-from&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#where-this-came-from&quot; aria-label=&quot;Anchor link for: where-this-came-from&quot;&gt;Where this came from&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;figure class=&quot;drawing wide&quot; style=&quot;--dw: 1154px;&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;chfp&#x2F;drawings&#x2F;fp-timeline-light.svg?h=1279d766a8a44f53afd7&quot;
         alt=&quot;7 era cards joined by arrows from the 1941 Z3 to 2024 FP4, each carrying a small orange bar of its bits per number drawn to one scale: 22, 32, 16, 16, 8, 4.25 and 4; a dashed card marks 1972 G.711, an 8-bit float in every phone call 50 years before FP8; FP4 carries the note 2025, pretrained at 10T tokens&quot; width=&quot;1154&quot; height=&quot;249&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;chfp&#x2F;drawings&#x2F;fp-timeline-dark.svg?h=f114ea4faef9fef8f2e7&quot;
         alt=&quot;7 era cards joined by arrows from the 1941 Z3 to 2024 FP4, each carrying a small orange bar of its bits per number drawn to one scale: 22, 32, 16, 16, 8, 4.25 and 4; a dashed card marks 1972 G.711, an 8-bit float in every phone call 50 years before FP8; FP4 carries the note 2025, pretrained at 10T tokens&quot; width=&quot;1154&quot; height=&quot;249&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 60.&lt;&#x2F;span&gt; 80 years of the same idea: use fewer bits where the numbers allow it, and move the repair (bias, scale, block) somewhere cheaper.&lt;&#x2F;figcaption&gt;
&lt;&#x2F;figure&gt;
&lt;p&gt;Konrad Zuse&#x27;s Z3 computed in 22-bit floating point in 1941,
special values included. 44 years later IEEE 754, largely
William Kahan&#x27;s design, ended an era in which every vendor&#x27;s
arithmetic disagreed &lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-3&quot;&gt;3&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;. The formats since bfloat16
are the same standard&#x27;s ideas at new bit counts, and the full table
of every named format that ever shipped, from the Z3 to the
G.711 telephone float of 1972 (an 8-bit float in every phone call
for 50 years) to NVFP4, is longer than this page and belongs
to a follow-up table of its own. The roads not taken, posits and
logarithmic number systems, are worth reading about
&lt;span class=&quot;cite&quot;&gt;[&lt;a href=&quot;#ref-17&quot;&gt;17&lt;&#x2F;a&gt;]&lt;&#x2F;span&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;build-it-yourself&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#build-it-yourself&quot; aria-label=&quot;Anchor link for: build-it-yourself&quot;&gt;Build it yourself&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;You already did, in the proofs. The 15-line decoder in
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p2-e4m3-map&quot;&gt;p2_e4m3_map.py&lt;&#x2F;a&gt; is a complete FP8
implementation that torch agrees with on all 256 patterns; the
block quantizer in &lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p11-mxblock&quot;&gt;p11_mxblock.py&lt;&#x2F;a&gt; is MXFP4
minus the packing; the stochastic rounder lives in
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p12-sr-drift&quot;&gt;p12_sr_drift.py&lt;&#x2F;a&gt;. Change E4M3&#x27;s constants
to E5M2&#x27;s and check yourself against torch again: that is the
exercise that makes every format in this family yours.&lt;&#x2F;p&gt;
&lt;details class=&quot;proof-fold&quot; id=&quot;proof-p11-mxblock&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;p11_mxblock.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: an MX-style block quantizer built from the spec: 32&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;elements share one power-of-two scale (E8M0), elements are E2M1.&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 scale exponent is rounded UP, the choice that keeps the block&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;maximum representable (the OCP round-down loses it). Sweep block&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-string&quot;&gt;sizes to see the trade: overhead per element vs error.&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; math&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;&#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-constant&quot;&gt;E2M1&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;0.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.5&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.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; 1.5&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.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.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; 4.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; 6.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-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; round_e2m1&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;v&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;    idx&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;v&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;unsqueeze&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-keyword z-operator&quot;&gt; -&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; E2M1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;argmin&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;    return&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt; E2M1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;idx&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;sign&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-storage z-type&quot;&gt;def&lt;&#x2F;span&gt;&lt;span class=&quot;z-entity z-name z-function&quot;&gt; mx_quant&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;x&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; block&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;32&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;    q&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;empty_like&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;&#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 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-support&quot;&gt; len&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; block&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; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;block&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;        amax&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&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;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;max&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; amax&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; 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;            q&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;block&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; 0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword&quot;&gt;continue&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        e&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ceil&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log2&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;amax&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; 6.0&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;     # round UP: amax stays &amp;lt;= 6*2^e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        e&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-keyword z-operator&quot;&gt;-&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt;127&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;127&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;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        s&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; **&lt;&#x2F;span&gt;&lt;span&gt; e&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        q&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;block&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation z-keyword z-operator z-assignment&quot;&gt;] =&lt;&#x2F;span&gt;&lt;span&gt; round_e2m1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;b&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; s&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; s&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&gt; q&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;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;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;4096&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; block&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-constant z-numeric&quot;&gt;8&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; 16&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; 32&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;    q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; mx_quant&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; block&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;    rel&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;q&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&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;abs&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; x&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;abs&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;().&lt;&#x2F;span&gt;&lt;span&gt;clamp_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;1e-9&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;)).&lt;&#x2F;span&gt;&lt;span&gt;median&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;    bits&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 class=&quot;z-constant z-numeric&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; block&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;block &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;block&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:&amp;gt;3&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;}&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;:  bits&#x2F;element &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;bits&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.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;   median rel err &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;rel&lt;&#x2F;span&gt;&lt;span class=&quot;z-storage z-type&quot;&gt;:.1%&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;# and the rounding-direction subtlety, demonstrated&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;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;5.9&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; 32&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;e_up&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;ceil&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log2&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.9&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; 6.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;))&lt;&#x2F;span&gt;&lt;span&gt;; e_dn&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;floor&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;math&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;log2&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.9&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; 6.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;up&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator z-assignment&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; round_e2m1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;b&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;e_up&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;e_up&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dn&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;round_e2m1&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;((&lt;&#x2F;span&gt;&lt;span&gt;b&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; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;e_dn&lt;&#x2F;span&gt;&lt;span class=&quot;z-punctuation&quot;&gt;).&lt;&#x2F;span&gt;&lt;span&gt;clamp&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;6&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-keyword z-operator&quot;&gt; *&lt;&#x2F;span&gt;&lt;span class=&quot;z-constant z-numeric&quot;&gt; 2.0&lt;&#x2F;span&gt;&lt;span class=&quot;z-keyword z-operator&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;e_dn&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;\n&lt;&#x2F;span&gt;&lt;span class=&quot;z-string&quot;&gt;block of 5.9s: scale rounded up -&amp;gt; &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;up&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;span class=&quot;z-storage z-type&quot;&gt;:.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;, &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;rounded down (clamped) -&amp;gt; &lt;&#x2F;span&gt;&lt;span class=&quot;z-constant&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;dn&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;span class=&quot;z-storage z-type&quot;&gt;:.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;   (true 5.90)&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;chfp&#x2F;proofs&#x2F;p11_mxblock.py&quot; download&gt;download and run it&lt;&#x2F;a&gt;
  &lt;&#x2F;div&gt;
&lt;&#x2F;details&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;ul&gt;
&lt;li&gt;why 0.1 + 0.2 misses, and which 3 numbers were never stored&lt;&#x2F;li&gt;
&lt;li&gt;how to convert 0.1 to binary by hand, and why its bits repeat&lt;&#x2F;li&gt;
&lt;li&gt;what the exponent and mantissa each buy, what the bias is for,
and why the leading 1 is never stored&lt;&#x2F;li&gt;
&lt;li&gt;how to read any format&#x27;s map: points per interval, step per interval,
ulp, ceiling, floor, ramp&lt;&#x2F;li&gt;
&lt;li&gt;every format&#x27;s card: where it came from, where it runs, and
the number at which it fails&lt;&#x2F;li&gt;
&lt;li&gt;the whole family on one sheet: how the bits split between
reach and digits, from float64&#x27;s 2,098 doublings to FP4&#x27;s 3.6&lt;&#x2F;li&gt;
&lt;li&gt;why bfloat16 is float32&#x27;s top 16 bits, at the bit level&lt;&#x2F;li&gt;
&lt;li&gt;what absorption, cancellation and order-dependence do to real
code, and which repair fits which&lt;&#x2F;li&gt;
&lt;li&gt;every epsilon in a transformer, and the one-line softmax slide&lt;&#x2F;li&gt;
&lt;li&gt;why training keeps a float32 master copy, and what loss scaling
actually moves&lt;&#x2F;li&gt;
&lt;li&gt;why FP8 is a pair, what a block scale is, why E8M0 has no
mantissa, and why FP4 cannot live without blocks&lt;&#x2F;li&gt;
&lt;li&gt;what stochastic rounding and Hadamard rotations buy at 4 bits&lt;&#x2F;li&gt;
&lt;li&gt;why INT4, FP4 and NF4 put their 16 points in different
places&lt;&#x2F;li&gt;
&lt;li&gt;why two GPUs never agree to the last bit, why even a compiler&#x27;s
fusion choice can change a program&#x27;s bits, and why neither is
a bug&lt;&#x2F;li&gt;
&lt;&#x2F;ul&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;Predict first, then run, then explain the difference:
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p0-riddles&quot;&gt;p0&lt;&#x2F;a&gt; the two opening questions;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p1-where-numbers-live&quot;&gt;p1&lt;&#x2F;a&gt; your own histogram;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p2-e4m3-map&quot;&gt;p2&lt;&#x2F;a&gt; all 256 of E4M3;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p4-absorb-cancel&quot;&gt;p4&lt;&#x2F;a&gt; absorption and cancellation;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p5-sum-order&quot;&gt;p5&lt;&#x2F;a&gt; order;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p6-stability&quot;&gt;p6&lt;&#x2F;a&gt; the cliff;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p7-sixteen-bits&quot;&gt;p7&lt;&#x2F;a&gt; the fork;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p8-lost-update&quot;&gt;p8&lt;&#x2F;a&gt; the vanished update;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p9-loss-scaling&quot;&gt;p9&lt;&#x2F;a&gt; the slide;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p10-fp4-outlier&quot;&gt;p10&lt;&#x2F;a&gt; the spike;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p11-mxblock&quot;&gt;p11&lt;&#x2F;a&gt; blocks;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p12-sr-drift&quot;&gt;p12&lt;&#x2F;a&gt; stochastic rounding;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p13-hadamard&quot;&gt;p13&lt;&#x2F;a&gt; the rotation;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p14-grids&quot;&gt;p14&lt;&#x2F;a&gt; the three grids;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p16-wide-formats&quot;&gt;p16&lt;&#x2F;a&gt; the four cards from two numbers;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p17-bf16-truncation&quot;&gt;p17&lt;&#x2F;a&gt; all 65,536 bfloat16 patterns;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p18-family-sheet&quot;&gt;p18&lt;&#x2F;a&gt; the family sheet;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p19-nvfp4-worked&quot;&gt;p19&lt;&#x2F;a&gt; one NVFP4 block by hand;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p20-e5m2-top-byte&quot;&gt;p20&lt;&#x2F;a&gt; all 256 E5M2 patterns;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p21-fma-one-rounding&quot;&gt;p21&lt;&#x2F;a&gt; 1 rounding against 2;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p22-exact-error&quot;&gt;p22&lt;&#x2F;a&gt; the error, caught exactly;
&lt;a href=&quot;https:&#x2F;&#x2F;tensor.khalilli.ai&#x2F;blog&#x2F;floating-point&#x2F;#proof-p23-double-rounding&quot;&gt;p23&lt;&#x2F;a&gt; rounding twice.
On paper: how many doublings separate E5M2&#x27;s floor from its
ceiling, and would the p1 gradients fit without a scale? And
before running p16: derive float16&#x27;s ceiling from E=5, M=10
yourself, the way the binary32 section taught you.&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;proof scripts for this page, measured on an Apple
M3 Max, torch 2.11.0, CPU&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; Sanglard, &lt;em&gt;Floating Point Visually Explained&lt;&#x2F;em&gt;, 2017. The
interval-and-position way of seeing the fields comes from here.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;fabiensanglard.net&#x2F;floating_point_visually_explained&#x2F;&quot;&gt;https:&#x2F;&#x2F;fabiensanglard.net&#x2F;floating_point_visually_explained&#x2F;&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; IEEE, &lt;em&gt;754-2019: Standard for Floating-Point Arithmetic&lt;&#x2F;em&gt;
(first edition 1985).&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; Goldberg, &lt;em&gt;What Every Computer Scientist Should Know About
Floating-Point Arithmetic&lt;&#x2F;em&gt;, ACM Computing Surveys, 1991; and Muller
et al., &lt;em&gt;Handbook of Floating-Point Arithmetic&lt;&#x2F;em&gt;, 2nd ed., 2018.&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; Micikevicius et al., &lt;em&gt;Mixed Precision Training&lt;&#x2F;em&gt;, ICLR 2018.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;1710.03740&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;1710.03740&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-6&quot;&gt;[6]&lt;&#x2F;span&gt; Micikevicius et al., &lt;em&gt;FP8 Formats for Deep Learning&lt;&#x2F;em&gt;, 2022.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2209.05433&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2209.05433&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-7&quot;&gt;[7]&lt;&#x2F;span&gt; DeepSeek-AI, &lt;em&gt;DeepSeek-V3 Technical Report&lt;&#x2F;em&gt;, 2024.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2412.19437&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2412.19437&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-8&quot;&gt;[8]&lt;&#x2F;span&gt; Dettmers et al., &lt;em&gt;LLM.int8(): 8-bit Matrix Multiplication
for Transformers at Scale&lt;&#x2F;em&gt;, 2022 (the emergent-outlier measurement).
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2208.07339&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2208.07339&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-9&quot;&gt;[9]&lt;&#x2F;span&gt; Open Compute Project, &lt;em&gt;Microscaling Formats (MX)
Specification v1.0&lt;&#x2F;em&gt;, 2023; and Rouhani et al., &lt;em&gt;Microscaling Data
Formats for Deep Learning&lt;&#x2F;em&gt;, 2023.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2310.10537&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2310.10537&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-10&quot;&gt;[10]&lt;&#x2F;span&gt; NVIDIA, &lt;em&gt;Pretraining Large Language Models with NVFP4&lt;&#x2F;em&gt;,
2025. &lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2509.25149&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2509.25149&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-11&quot;&gt;[11]&lt;&#x2F;span&gt; Mishra et al., &lt;em&gt;Recipes for Pre-training LLMs with MXFP8&lt;&#x2F;em&gt;,
2025 (the scale-rounding result).
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2506.08027&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2506.08027&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-12&quot;&gt;[12]&lt;&#x2F;span&gt; Tseng et al., &lt;em&gt;Training LLMs with MXFP4&lt;&#x2F;em&gt;, 2025.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2502.20586&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2502.20586&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-13&quot;&gt;[13]&lt;&#x2F;span&gt; Dettmers et al., &lt;em&gt;QLoRA: Efficient Finetuning of Quantized
LLMs&lt;&#x2F;em&gt;, 2023 (NF4). &lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2305.14314&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2305.14314&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-14&quot;&gt;[14]&lt;&#x2F;span&gt; Frantar et al., &lt;em&gt;GPTQ&lt;&#x2F;em&gt;, 2022.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2210.17323&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2210.17323&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-15&quot;&gt;[15]&lt;&#x2F;span&gt; Lin et al., &lt;em&gt;AWQ: Activation-aware Weight Quantization&lt;&#x2F;em&gt;,
2023. &lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2306.00978&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2306.00978&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-16&quot;&gt;[16]&lt;&#x2F;span&gt; Dettmers et al., &lt;em&gt;8-bit Optimizers via Block-wise
Quantization&lt;&#x2F;em&gt;, 2021.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2110.02861&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2110.02861&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-17&quot;&gt;[17]&lt;&#x2F;span&gt; Gustafson and Yonemoto, &lt;em&gt;Beating Floating Point at its Own
Game: Posit Arithmetic&lt;&#x2F;em&gt;, 2017; logarithmic number systems survey in
Muller et al. [4].&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-18&quot;&gt;[18]&lt;&#x2F;span&gt; Severance, &lt;em&gt;An Interview with the Old Man of
Floating-Point&lt;&#x2F;em&gt; (William Kahan on IEEE 754 and the Intel 8087), 1998.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;people.eecs.berkeley.edu&#x2F;~wkahan&#x2F;ieee754status&#x2F;754story.html&quot;&gt;https:&#x2F;&#x2F;people.eecs.berkeley.edu&#x2F;~wkahan&#x2F;ieee754status&#x2F;754story.html&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-19&quot;&gt;[19]&lt;&#x2F;span&gt; NVIDIA, &lt;em&gt;CUDA C++ Programming Guide&lt;&#x2F;em&gt;, the arithmetic
instructions throughput table (per-architecture float64 rates).
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;docs.nvidia.com&#x2F;cuda&#x2F;cuda-c-programming-guide&#x2F;&quot;&gt;https:&#x2F;&#x2F;docs.nvidia.com&#x2F;cuda&#x2F;cuda-c-programming-guide&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-20&quot;&gt;[20]&lt;&#x2F;span&gt; Kharya, &lt;em&gt;TensorFloat-32 in the A100 GPU Accelerates AI
Training, HPC up to 20x&lt;&#x2F;em&gt;, NVIDIA blog, 2020.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;blogs.nvidia.com&#x2F;blog&#x2F;tensorfloat-32-precision-format&#x2F;&quot;&gt;https:&#x2F;&#x2F;blogs.nvidia.com&#x2F;blog&#x2F;tensorfloat-32-precision-format&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-21&quot;&gt;[21]&lt;&#x2F;span&gt; PyTorch documentation, &lt;em&gt;CUDA semantics&lt;&#x2F;em&gt; (the TF32 flags and
their defaults since 1.12).
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;docs.pytorch.org&#x2F;docs&#x2F;stable&#x2F;notes&#x2F;cuda.html&quot;&gt;https:&#x2F;&#x2F;docs.pytorch.org&#x2F;docs&#x2F;stable&#x2F;notes&#x2F;cuda.html&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-22&quot;&gt;[22]&lt;&#x2F;span&gt; Industrial Light &amp;amp; Magic, &lt;em&gt;About OpenEXR&lt;&#x2F;em&gt; (the half type,
2000-2003, and its Cg compatibility); and Bogart, Kainz, Hess, &lt;em&gt;The
OpenEXR Image File Format&lt;&#x2F;em&gt;, GPU Gems, 2004.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;openexr.com&#x2F;en&#x2F;latest&#x2F;about.html&quot;&gt;https:&#x2F;&#x2F;openexr.com&#x2F;en&#x2F;latest&#x2F;about.html&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-23&quot;&gt;[23]&lt;&#x2F;span&gt; Wang and Kanwar, &lt;em&gt;BFloat16: The secret to high performance
on Cloud TPUs&lt;&#x2F;em&gt;, Google Cloud blog, 2019.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;cloud.google.com&#x2F;blog&#x2F;products&#x2F;ai-machine-learning&#x2F;bfloat16-the-secret-to-high-performance-on-cloud-tpus&quot;&gt;https:&#x2F;&#x2F;cloud.google.com&#x2F;blog&#x2F;products&#x2F;ai-machine-learning&#x2F;bfloat16-the-secret-to-high-performance-on-cloud-tpus&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-24&quot;&gt;[24]&lt;&#x2F;span&gt; Ku, Poli et al. (Radical Numerics), &lt;em&gt;NVFP4 pretraining:
from theory to implementation, Part 1&lt;&#x2F;em&gt;, 2026. The recipe
walk-through this page&#x27;s NVFP4 worked block follows.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;www.radicalnumerics.ai&#x2F;blog&#x2F;nvfp4-part1&quot;&gt;https:&#x2F;&#x2F;www.radicalnumerics.ai&#x2F;blog&#x2F;nvfp4-part1&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-25&quot;&gt;[25]&lt;&#x2F;span&gt; Easygoing, &lt;em&gt;Which is Better: FP8_scaled or MXFP8? A
Thorough Comparison of Image Generation AI Model Accuracy and
Speed&lt;&#x2F;em&gt;, AI Image Journey, 2026. The measured quality ranking and
the measured slowdown of MXFP8 on hardware without MX support.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;note.com&#x2F;ai_image_journey&#x2F;n&#x2F;n99d0ed2f1c1d&quot;&gt;https:&#x2F;&#x2F;note.com&#x2F;ai_image_journey&#x2F;n&#x2F;n99d0ed2f1c1d&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-26&quot;&gt;[26]&lt;&#x2F;span&gt; ZeroEntropy, &lt;em&gt;MXFP4&lt;&#x2F;em&gt; (concepts), 2026. The per-vendor
native-support summary.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;zeroentropy.dev&#x2F;concepts&#x2F;mxfp4&#x2F;&quot;&gt;https:&#x2F;&#x2F;zeroentropy.dev&#x2F;concepts&#x2F;mxfp4&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-27&quot;&gt;[27]&lt;&#x2F;span&gt; Boehm, &lt;em&gt;Can Function Inlining Affect Floating Point
Outputs? Exploring FMA and Other Consistency Issues&lt;&#x2F;em&gt;, 2023.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;siboehm.com&#x2F;articles&#x2F;23&#x2F;Inlining-FMA-FP-consistency&quot;&gt;https:&#x2F;&#x2F;siboehm.com&#x2F;articles&#x2F;23&#x2F;Inlining-FMA-FP-consistency&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-28&quot;&gt;[28]&lt;&#x2F;span&gt; PyTorch documentation, &lt;em&gt;Numerical accuracy&lt;&#x2F;em&gt;.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;docs.pytorch.org&#x2F;docs&#x2F;stable&#x2F;notes&#x2F;numerical_accuracy.html&quot;&gt;https:&#x2F;&#x2F;docs.pytorch.org&#x2F;docs&#x2F;stable&#x2F;notes&#x2F;numerical_accuracy.html&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-29&quot;&gt;[29]&lt;&#x2F;span&gt; Kwon et al., &lt;em&gt;Efficient Memory Management for Large Language
Model Serving with PagedAttention&lt;&#x2F;em&gt; (the vLLM paper), 2023.
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2309.06180&quot;&gt;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2309.06180&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;ref-num&quot; id=&quot;ref-30&quot;&gt;[30]&lt;&#x2F;span&gt; NVIDIA, &lt;em&gt;H100 Tensor Core GPU&lt;&#x2F;em&gt; specifications (HBM3 bandwidth
and PCIe generation 5 rates).
&lt;a class=&quot;ref-link&quot; href=&quot;https:&#x2F;&#x2F;www.nvidia.com&#x2F;en-us&#x2F;data-center&#x2F;h100&#x2F;&quot;&gt;https:&#x2F;&#x2F;www.nvidia.com&#x2F;en-us&#x2F;data-center&#x2F;h100&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Three good things to read after this page: Goldberg&#x27;s paper [4]
slowly, with the map in hand; the OCP MX specification [9], which is
short and readable; and Kahan&#x27;s interview on the making of IEEE 754
[18], where the standard&#x27;s designer tells the story himself.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <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>
