{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "execute:\n",
    "  enabled: false\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-1 fw=all -->\n",
    "\n",
    "# Numerics: Dtypes and Mixed Precision\n",
    "\n",
    "Every tensor carries three attributes: a shape, a device, and a *dtype*, the\n",
    "numeric format of its entries. So far we have left the dtype at its default,\n",
    "32-bit floating point, and nothing forced us to look at it. That grace period\n",
    "is over. Modern accelerators multiply 16-bit matrices several times faster\n",
    "than 32-bit ones, in half the memory, and essentially all serious training now\n",
    "chooses numeric formats deliberately. This section covers what a builder needs:\n",
    "which formats exist, what each one can and cannot represent, how dtypes combine,\n",
    "and the standard recipe (*mixed precision*) for training in 16 bits without\n",
    "giving up 32-bit accuracy. For the anatomy of a floating-point number (sign,\n",
    "exponent, mantissa) see that section;\n",
    "here we ask the practical questions: when does it break, and which switch do\n",
    "I flip."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-numerics-dtypes-and-mixed-precision"
   },
   "source": [
    "import torch\n",
    "import time\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "from d2l import torch as d2l"
   ],
   "execution_count": 1,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-2 fw=all -->\n",
    "\n",
    "## The Dtype Zoo\n",
    "\n",
    "Half-precision (`float16`, \"fp16\") sounds like a free lunch: half the\n",
    "bytes of fp32, and the format that accelerator hardware sped up first. Here\n",
    "is the catch. The largest number fp16 can represent is 65504. Square a value\n",
    "of 300, which is nothing exotic (an unnormalized logit, an intermediate in a\n",
    "variance computation), and you have already left the representable range:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-the-dtype-zoo-1"
   },
   "source": [
    "x = torch.tensor(300.0)\n",
    "x.to(torch.float16)**2, x.to(torch.bfloat16)**2"
   ],
   "execution_count": 2,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(tensor(inf, dtype=torch.float16), tensor(90112., dtype=torch.bfloat16))"
     },
     "metadata": {},
     "execution_count": 2
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-3 fw=all -->\n",
    "\n",
    "fp16 overflows to `inf`. The second format, `bfloat16` (\"bf16\", brain\n",
    "floating point), returns 90112: wrong in the fourth digit, since the exact\n",
    "square is 90000, but finite. The two formats spend the same 16 bits\n",
    "differently. fp16 uses 5 exponent bits and 10 mantissa bits: fine-grained\n",
    "steps, tiny range. bf16 keeps fp32's full 8 exponent bits and pays with a\n",
    "7-bit mantissa: fp32's range, coarse steps. The one-line demo above shows both\n",
    "consequences at once.\n",
    "\n",
    "\n",
    "| format | sign | exponent | mantissa |\n",
    "|:-------|-----:|---------:|---------:|\n",
    "| fp32   | 1    | 8        | 23       |\n",
    "| tf32   | 1    | 8        | 10       |\n",
    "| bf16   | 1    | 8        | 7        |\n",
    "| fp16   | 1    | 5        | 10       |\n",
    "| fp8 (e4m3) | 1 | 4       | 3        |\n",
    "\n",
    "the figure draws the same table as bit layouts, aligned\n",
    "at the sign bit so the total widths compare directly: fp32 spends its 32 bits\n",
    "on a wide mantissa, while bf16 keeps fp32's 8-bit exponent but pays for it\n",
    "with a narrow 7-bit mantissa, and fp16 inverts the trade.\n",
    "\n",
    "![Bit layouts of the five formats in the table above, aligned at the sign bit so a wider bar means more total bits: fp32 is the widest, tf32 shares fp32's exponent but truncates the mantissa to 10 bits, and fp16 trades exponent bits for mantissa bits that bf16 spends the other way.](../img/bg-float-formats.svg)\n",
    "\n",
    "`finfo` reports what each bit budget buys. Three numbers matter: `max`,\n",
    "the overflow threshold; `tiny`, the smallest *normal* value, below which\n",
    "subnormal values provide a short, progressively less precise tail before zero;\n",
    "and `eps`, the relative step size between adjacent representable values near 1."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-the-dtype-zoo-2"
   },
   "source": [
    "for dtype in (torch.float32, torch.bfloat16, torch.float16):\n",
    "    fi = torch.finfo(dtype)\n",
    "    print(f'{str(dtype):15} max {fi.max:10.3e}  tiny {fi.tiny:9.3e}'\n",
    "          f'  eps {fi.eps:8.3e}')"
   ],
   "execution_count": 3,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "torch.float32   max  3.403e+38  tiny 1.175e-38  eps 1.192e-07\ntorch.bfloat16  max  3.390e+38  tiny 1.175e-38  eps 7.812e-03\ntorch.float16   max  6.550e+04  tiny 6.104e-05  eps 9.766e-04\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-4 fw=all -->\n",
    "\n",
    "bf16 matches fp32's `tiny` and has nearly the same `max`: its shared exponent\n",
    "width gives the same normal exponent range, while its shorter mantissa makes\n",
    "the largest finite value slightly smaller. Its `eps` of 0.0078 means two to\n",
    "three significant decimal digits. fp16 resolves about three to four digits,\n",
    "overflows at 65504, enters the subnormal range below about\n",
    "$6.10 \\times 10^{-5}$, and reaches zero only below about\n",
    "$5.96 \\times 10^{-8}$. The trade is precision against range, and for deep\n",
    "learning the choice is lopsided: activations and gradients span many orders\n",
    "of magnitude, occasional large values are routine, and running out of range\n",
    "produces `inf` while losing a low-order digit usually costs nothing a noisy\n",
    "gradient estimate had to offer anyway. That asymmetry is why bf16 became the\n",
    "default 16-bit format for training.\n",
    "\n",
    "### TF32: What Happens to fp32 Matrix Multiplication\n",
    "\n",
    "The second row of the table, tf32, is not a storage dtype; you cannot create a\n",
    "tensor of it. It is a compute mode of NVIDIA tensor cores (Ampere generation\n",
    "and later): matrix-multiply inputs are rounded to a 10-bit mantissa while\n",
    "keeping fp32's exponent, and products are accumulated in fp32. Your tensors\n",
    "stay fp32; only the arithmetic inside the multiplication runs faster and\n",
    "slightly less precisely. Most of our frameworks expose a switch for it:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-tf32-what-happens-to-fp32-matrix-multiplication"
   },
   "source": [
    "print(torch.get_float32_matmul_precision())\n",
    "torch.set_float32_matmul_precision('high')  # allow tf32 in fp32 matmuls\n",
    "print(torch.get_float32_matmul_precision(),\n",
    "      torch.backends.cuda.matmul.allow_tf32)\n",
    "torch.set_float32_matmul_precision('highest')  # restore the default"
   ],
   "execution_count": 4,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "highest\nhigh True\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-5 fw=pytorch -->\n",
    "\n",
    "The default is `'highest'`: fp32 matrix multiplications compute in true fp32\n",
    "and the tensor-core shortcut stays off. Setting `'high'` opts in (it also\n",
    "flips the older `allow_tf32` flag; they are two views of one setting). The\n",
    "history is a trap for readers of old tutorials: PyTorch 1.7 through 1.11\n",
    "enabled tf32 *by default*, and the default changed to off in 1.12 without a\n",
    "warning. Material from that era asserts that tf32 is automatic on Ampere GPUs;\n",
    "on any current version it is not, and the check above is how you find out what\n",
    "your process is actually doing. For training, `'high'` is generally safe\n",
    "(products still accumulate in fp32) and substantially faster on tensor-core\n",
    "hardware; keep `'highest'` for ill-conditioned linear algebra or when\n",
    "reproducing results bit for bit."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-6 fw=all -->\n",
    "\n",
    "### Below 16 Bits\n",
    "\n",
    "Production inference pushes further down: int8 quantization is standard for\n",
    "serving, and fp8 training (a 4-bit-exponent variant for the forward pass, a\n",
    "5-bit-exponent variant for gradients, with per-tensor scaling) runs on\n",
    "H100-class hardware [@Micikevicius.Stosic.Burgess.ea.2022]. Both require\n",
    "calibration machinery beyond a dtype argument, so for this book 16 bits is the\n",
    "floor; the exercises let you inspect the fp8 format's `finfo`.\n",
    "\n",
    "\n",
    "## Dtype Rules: Promotion, Parameters, and Casts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-7 fw=pytorch -->\n",
    "\n",
    "What happens when dtypes meet in one expression? For plain tensors, PyTorch\n",
    "promotes to the type that can represent both:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-dtype-rules-promotion-parameters-and-casts-1"
   },
   "source": [
    "x16 = torch.ones(3, dtype=torch.float16)\n",
    "x32 = torch.ones(3, dtype=torch.float32)\n",
    "(x16 + x32).dtype, (x16 + 1.0).dtype, (x16 + torch.arange(3)).dtype"
   ],
   "execution_count": 5,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(torch.float32, torch.float16, torch.float16)"
     },
     "metadata": {},
     "execution_count": 5
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-8 fw=pytorch, jax -->\n",
    "\n",
    "Mixing two float tensors upcasts to the wider one, so an fp16 pipeline with a\n",
    "stray fp32 tensor quietly becomes fp32 from that point on, doubling downstream\n",
    "memory. Python scalars and integer tensors are *weak*: they adopt the float\n",
    "tensor's dtype instead of dragging it up, which is why sprinkling literals\n",
    "like `x + 1.0` into low-precision code is harmless. (Mixing fp16 with bf16\n",
    "promotes to fp32, since neither contains the other.)"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-dtype-rules-promotion-parameters-and-casts-2"
   },
   "source": [
    "net = nn.Sequential(nn.Flatten(), nn.Linear(784, 256), nn.ReLU(),\n",
    "                    nn.Linear(256, 10))\n",
    "def param_bytes(net):\n",
    "    return sum(p.numel() * p.element_size() for p in net.parameters())\n",
    "param_bytes(net)"
   ],
   "execution_count": 6,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "814120"
     },
     "metadata": {},
     "execution_count": 6
    }
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-dtype-rules-promotion-parameters-and-casts-3"
   },
   "source": [
    "net = net.bfloat16()\n",
    "X = torch.randn(8, 1, 28, 28)\n",
    "try:\n",
    "    net(X)\n",
    "except RuntimeError as e:\n",
    "    print(e)\n",
    "print(net(X.bfloat16()).dtype, param_bytes(net))"
   ],
   "execution_count": 7,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "mat1 and mat2 must have the same dtype, but got Float and BFloat16\ntorch.bfloat16 407060\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-9 fw=pytorch -->\n",
    "\n",
    "The parameter footprint halves, inference works, and the error message shows\n",
    "the strictness: the fp32 input was refused rather than silently converted."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-10 fw=all -->\n",
    "\n",
    "Casting the model like this is the right tool for *inference*: half the\n",
    "memory, no gradients to worry about, and a rounding error in the forward pass\n",
    "rarely changes an argmax. For *training* it is a trap. A single optimizer\n",
    "update changes a weight by roughly $\\eta \\cdot g$, often a factor $10^{-4}$\n",
    "or less of the weight's own magnitude, and adding an increment smaller than\n",
    "about `eps` times the weight rounds to no change at all. With bf16's `eps` of\n",
    "0.0078, small updates evaporate and learning stalls; in fp16 the small\n",
    "gradients themselves flush to zero first. Hence the rule, and it resolves the\n",
    "single most common confusion in practice:\n",
    "\n",
    "**Cast the model for inference. For training, keep fp32 weights and run the\n",
    "compute in 16 bits.**\n",
    "\n",
    "## Mixed-Precision Training\n",
    "\n",
    "Mixed-precision training [@Micikevicius.Narang.Alben.ea.2018] splits the\n",
    "work: parameters stay in fp32 (the *master weights*, so that small updates\n",
    "still register), while the expensive operations of the forward and backward\n",
    "pass run in a 16-bit dtype."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-11 fw=pytorch -->\n",
    "\n",
    "You do not annotate anything per layer. Inside a\n",
    "`torch.autocast` context, each eligible operation consults a built-in policy:\n",
    "matrix multiplications and convolutions, which dominate compute and map onto\n",
    "tensor cores, run in the low dtype, while selected sensitive operations such\n",
    "as cross-entropy run in fp32. Unlisted operations run in their input dtype, so\n",
    "a custom reduction over a low-precision tensor still needs an explicit fp32\n",
    "cast. PyTorch maintains the eligibility lists, and inputs are cast on the fly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-12 fw=all -->\n",
    "\n",
    "the figure draws the resulting\n",
    "loop: this is the distinction that matters between casting a whole model\n",
    "(everything in one dtype) and mixed precision (fp32 master\n",
    "weights that a 16-bit forward and backward pass read from and write back to).\n",
    "\n",
    "![The mixed-precision training loop: fp32 master weights are cast to bf16 for the forward pass and its bf16 activations, the loss accumulates back in fp32, the backward pass produces bf16 gradients, and the optimizer step reads those gradients but updates the fp32 master copy, closing the cycle; the fp16 variant additionally scales the loss up before backward and unscales the gradients back down before the step.](../img/bg-amp-loop.svg)"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-mixed-precision-training-1"
   },
   "source": [
    "net = nn.Sequential(nn.Flatten(), nn.Linear(784, 256), nn.ReLU(),\n",
    "                    nn.Linear(256, 10))\n",
    "with torch.autocast('cpu', dtype=torch.bfloat16):\n",
    "    Y = net(X)\n",
    "Y.dtype, net[1].weight.dtype"
   ],
   "execution_count": 8,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(torch.bfloat16, torch.float32)"
     },
     "metadata": {},
     "execution_count": 8
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-13 fw=pytorch -->\n",
    "\n",
    "Compute in bf16, storage in fp32: exactly the opposite split from\n",
    "`net.bfloat16()`. The first argument names the device type the computation\n",
    "runs on; on a GPU you would write `torch.autocast('cuda', ...)` and nothing\n",
    "else changes."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-14 fw=all -->\n",
    "\n",
    "Let us verify the claim that matters, that accuracy survives.\n",
    "We train the same MLP on Fashion-MNIST twice, once in fp32 and once with\n",
    "16-bit compute, from the same initialization and on the same batches:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-mixed-precision-training-2"
   },
   "source": [
    "data = d2l.FashionMNIST(batch_size=256)\n",
    "loader = torch.utils.data.DataLoader(data.train, batch_size=256)\n",
    "batches = [b for b, _ in zip(loader, range(100))]\n",
    "val_batch = next(iter(torch.utils.data.DataLoader(data.val, batch_size=1024)))"
   ],
   "execution_count": 9,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-mixed-precision-training-3"
   },
   "source": [
    "def train(amp):\n",
    "    torch.manual_seed(0)  # identical init and data order for both runs\n",
    "    net = nn.Sequential(nn.Flatten(), nn.Linear(784, 256), nn.ReLU(),\n",
    "                        nn.Linear(256, 10))\n",
    "    opt = torch.optim.SGD(net.parameters(), lr=0.1)\n",
    "    losses = []\n",
    "    start = time.perf_counter()\n",
    "    for X, y in batches:\n",
    "        with torch.autocast('cpu', dtype=torch.bfloat16, enabled=amp):\n",
    "            loss = F.cross_entropy(net(X), y)\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        opt.zero_grad()\n",
    "        losses.append(loss.item())\n",
    "    elapsed = time.perf_counter() - start\n",
    "    Xv, yv = val_batch\n",
    "    with torch.no_grad():\n",
    "        accuracy = (net(Xv).argmax(1) == yv).float().mean().item()\n",
    "    return losses, accuracy, elapsed"
   ],
   "execution_count": 10,
   "outputs": []
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-mixed-precision-training-4"
   },
   "source": [
    "loss32, acc32, sec32 = train(amp=False)\n",
    "loss16, acc16, sec16 = train(amp=True)\n",
    "print(f'fp32: loss {loss32[-1]:.3f}, val acc {acc32:.3f}, {sec32:.2f}s')\n",
    "print(f'bf16: loss {loss16[-1]:.3f}, val acc {acc16:.3f}, {sec16:.2f}s')\n",
    "d2l.plot(list(range(1, 101)), [loss32, loss16], 'step', 'loss',\n",
    "         legend=['fp32', 'bf16 autocast'])"
   ],
   "execution_count": 11,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "fp32: loss 0.815, val acc 0.737, 0.42s\nbf16: loss 0.815, val acc 0.738, 1.45s\n"
     ]
    },
    {
     "output_type": "display_data",
     "data": {
      "image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"246.947727pt\" height=\"183.35625pt\" viewBox=\"0 0 246.947727 183.35625\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n <metadata>\n  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n   <cc:Work>\n    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n    <dc:date>2026-07-19T13:40:12.575407</dc:date>\n    <dc:format>image/svg+xml</dc:format>\n    <dc:creator>\n     <cc:Agent>\n      <dc:title>Matplotlib v3.10.8, https://matplotlib.org/</dc:title>\n     </cc:Agent>\n    </dc:creator>\n   </cc:Work>\n  </rdf:RDF>\n </metadata>\n <defs>\n  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n </defs>\n <g id=\"figure_1\">\n  <g id=\"patch_1\">\n   <path d=\"M 0 183.35625 \nL 246.947727 183.35625 \nL 246.947727 0 \nL 0 0 \nz\n\" style=\"fill: #ffffff\"/>\n  </g>\n  <g id=\"axes_1\">\n   <g id=\"patch_2\">\n    <path d=\"M 43.78125 145.8 \nL 239.08125 145.8 \nL 239.08125 7.2 \nL 43.78125 7.2 \nz\n\" style=\"fill: #ffffff\"/>\n   </g>\n   <g id=\"matplotlib.axis_1\">\n    <g id=\"xtick_1\">\n     <g id=\"line2d_1\">\n      <path d=\"M 50.865134 145.8 \nL 50.865134 7.2 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_2\">\n      <defs>\n       <path id=\"md91a3fa7f3\" d=\"M 0 0 \nL 0 3.5 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#md91a3fa7f3\" x=\"50.865134\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_1\">\n      <!-- 0 -->\n      <g transform=\"translate(47.683884 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-30\" d=\"M 2034 4250 \nQ 1547 4250 1301 3770 \nQ 1056 3291 1056 2328 \nQ 1056 1369 1301 889 \nQ 1547 409 2034 409 \nQ 2525 409 2770 889 \nQ 3016 1369 3016 2328 \nQ 3016 3291 2770 3770 \nQ 2525 4250 2034 4250 \nz\nM 2034 4750 \nQ 2819 4750 3233 4129 \nQ 3647 3509 3647 2328 \nQ 3647 1150 3233 529 \nQ 2819 -91 2034 -91 \nQ 1250 -91 836 529 \nQ 422 1150 422 2328 \nQ 422 3509 836 4129 \nQ 1250 4750 2034 4750 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-30\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_2\">\n     <g id=\"line2d_3\">\n      <path d=\"M 86.732903 145.8 \nL 86.732903 7.2 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_4\">\n      <g>\n       <use xlink:href=\"#md91a3fa7f3\" x=\"86.732903\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_2\">\n      <!-- 20 -->\n      <g transform=\"translate(80.370403 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-32\" d=\"M 1228 531 \nL 3431 531 \nL 3431 0 \nL 469 0 \nL 469 531 \nQ 828 903 1448 1529 \nQ 2069 2156 2228 2338 \nQ 2531 2678 2651 2914 \nQ 2772 3150 2772 3378 \nQ 2772 3750 2511 3984 \nQ 2250 4219 1831 4219 \nQ 1534 4219 1204 4116 \nQ 875 4013 500 3803 \nL 500 4441 \nQ 881 4594 1212 4672 \nQ 1544 4750 1819 4750 \nQ 2544 4750 2975 4387 \nQ 3406 4025 3406 3419 \nQ 3406 3131 3298 2873 \nQ 3191 2616 2906 2266 \nQ 2828 2175 2409 1742 \nQ 1991 1309 1228 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_3\">\n     <g id=\"line2d_5\">\n      <path d=\"M 122.600671 145.8 \nL 122.600671 7.2 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_6\">\n      <g>\n       <use xlink:href=\"#md91a3fa7f3\" x=\"122.600671\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_3\">\n      <!-- 40 -->\n      <g transform=\"translate(116.238171 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-34\" d=\"M 2419 4116 \nL 825 1625 \nL 2419 1625 \nL 2419 4116 \nz\nM 2253 4666 \nL 3047 4666 \nL 3047 1625 \nL 3713 1625 \nL 3713 1100 \nL 3047 1100 \nL 3047 0 \nL 2419 0 \nL 2419 1100 \nL 313 1100 \nL 313 1709 \nL 2253 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-34\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_4\">\n     <g id=\"line2d_7\">\n      <path d=\"M 158.46844 145.8 \nL 158.46844 7.2 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_8\">\n      <g>\n       <use xlink:href=\"#md91a3fa7f3\" x=\"158.46844\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_4\">\n      <!-- 60 -->\n      <g transform=\"translate(152.10594 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-36\" d=\"M 2113 2584 \nQ 1688 2584 1439 2293 \nQ 1191 2003 1191 1497 \nQ 1191 994 1439 701 \nQ 1688 409 2113 409 \nQ 2538 409 2786 701 \nQ 3034 994 3034 1497 \nQ 3034 2003 2786 2293 \nQ 2538 2584 2113 2584 \nz\nM 3366 4563 \nL 3366 3988 \nQ 3128 4100 2886 4159 \nQ 2644 4219 2406 4219 \nQ 1781 4219 1451 3797 \nQ 1122 3375 1075 2522 \nQ 1259 2794 1537 2939 \nQ 1816 3084 2150 3084 \nQ 2853 3084 3261 2657 \nQ 3669 2231 3669 1497 \nQ 3669 778 3244 343 \nQ 2819 -91 2113 -91 \nQ 1303 -91 875 529 \nQ 447 1150 447 2328 \nQ 447 3434 972 4092 \nQ 1497 4750 2381 4750 \nQ 2619 4750 2861 4703 \nQ 3103 4656 3366 4563 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-36\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_5\">\n     <g id=\"line2d_9\">\n      <path d=\"M 194.336209 145.8 \nL 194.336209 7.2 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_10\">\n      <g>\n       <use xlink:href=\"#md91a3fa7f3\" x=\"194.336209\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_5\">\n      <!-- 80 -->\n      <g transform=\"translate(187.973709 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-38\" d=\"M 2034 2216 \nQ 1584 2216 1326 1975 \nQ 1069 1734 1069 1313 \nQ 1069 891 1326 650 \nQ 1584 409 2034 409 \nQ 2484 409 2743 651 \nQ 3003 894 3003 1313 \nQ 3003 1734 2745 1975 \nQ 2488 2216 2034 2216 \nz\nM 1403 2484 \nQ 997 2584 770 2862 \nQ 544 3141 544 3541 \nQ 544 4100 942 4425 \nQ 1341 4750 2034 4750 \nQ 2731 4750 3128 4425 \nQ 3525 4100 3525 3541 \nQ 3525 3141 3298 2862 \nQ 3072 2584 2669 2484 \nQ 3125 2378 3379 2068 \nQ 3634 1759 3634 1313 \nQ 3634 634 3220 271 \nQ 2806 -91 2034 -91 \nQ 1263 -91 848 271 \nQ 434 634 434 1313 \nQ 434 1759 690 2068 \nQ 947 2378 1403 2484 \nz\nM 1172 3481 \nQ 1172 3119 1398 2916 \nQ 1625 2713 2034 2713 \nQ 2441 2713 2670 2916 \nQ 2900 3119 2900 3481 \nQ 2900 3844 2670 4047 \nQ 2441 4250 2034 4250 \nQ 1625 4250 1398 4047 \nQ 1172 3844 1172 3481 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-38\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"xtick_6\">\n     <g id=\"line2d_11\">\n      <path d=\"M 230.203977 145.8 \nL 230.203977 7.2 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_12\">\n      <g>\n       <use xlink:href=\"#md91a3fa7f3\" x=\"230.203977\" y=\"145.8\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_6\">\n      <!-- 100 -->\n      <g transform=\"translate(220.660227 160.398438) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-31\" d=\"M 794 531 \nL 1825 531 \nL 1825 4091 \nL 703 3866 \nL 703 4441 \nL 1819 4666 \nL 2450 4666 \nL 2450 531 \nL 3481 531 \nL 3481 0 \nL 794 0 \nL 794 531 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(127.246094 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_7\">\n     <!-- step -->\n     <g transform=\"translate(130.615625 174.076563) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-73\" d=\"M 2834 3397 \nL 2834 2853 \nQ 2591 2978 2328 3040 \nQ 2066 3103 1784 3103 \nQ 1356 3103 1142 2972 \nQ 928 2841 928 2578 \nQ 928 2378 1081 2264 \nQ 1234 2150 1697 2047 \nL 1894 2003 \nQ 2506 1872 2764 1633 \nQ 3022 1394 3022 966 \nQ 3022 478 2636 193 \nQ 2250 -91 1575 -91 \nQ 1294 -91 989 -36 \nQ 684 19 347 128 \nL 347 722 \nQ 666 556 975 473 \nQ 1284 391 1588 391 \nQ 1994 391 2212 530 \nQ 2431 669 2431 922 \nQ 2431 1156 2273 1281 \nQ 2116 1406 1581 1522 \nL 1381 1569 \nQ 847 1681 609 1914 \nQ 372 2147 372 2553 \nQ 372 3047 722 3315 \nQ 1072 3584 1716 3584 \nQ 2034 3584 2315 3537 \nQ 2597 3491 2834 3397 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-74\" d=\"M 1172 4494 \nL 1172 3500 \nL 2356 3500 \nL 2356 3053 \nL 1172 3053 \nL 1172 1153 \nQ 1172 725 1289 603 \nQ 1406 481 1766 481 \nL 2356 481 \nL 2356 0 \nL 1766 0 \nQ 1100 0 847 248 \nQ 594 497 594 1153 \nL 594 3053 \nL 172 3053 \nL 172 3500 \nL 594 3500 \nL 594 4494 \nL 1172 4494 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-65\" d=\"M 3597 1894 \nL 3597 1613 \nL 953 1613 \nQ 991 1019 1311 708 \nQ 1631 397 2203 397 \nQ 2534 397 2845 478 \nQ 3156 559 3463 722 \nL 3463 178 \nQ 3153 47 2828 -22 \nQ 2503 -91 2169 -91 \nQ 1331 -91 842 396 \nQ 353 884 353 1716 \nQ 353 2575 817 3079 \nQ 1281 3584 2069 3584 \nQ 2775 3584 3186 3129 \nQ 3597 2675 3597 1894 \nz\nM 3022 2063 \nQ 3016 2534 2758 2815 \nQ 2500 3097 2075 3097 \nQ 1594 3097 1305 2825 \nQ 1016 2553 972 2059 \nL 3022 2063 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-70\" d=\"M 1159 525 \nL 1159 -1331 \nL 581 -1331 \nL 581 3500 \nL 1159 3500 \nL 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nz\nM 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-73\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(52.099609 0)\"/>\n      <use xlink:href=\"#DejaVuSans-65\" transform=\"translate(91.308594 0)\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(152.832031 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"matplotlib.axis_2\">\n    <g id=\"ytick_1\">\n     <g id=\"line2d_13\">\n      <path d=\"M 43.78125 113.019476 \nL 239.08125 113.019476 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_14\">\n      <defs>\n       <path id=\"m6c6813ab19\" d=\"M 0 0 \nL -3.5 0 \n\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </defs>\n      <g>\n       <use xlink:href=\"#m6c6813ab19\" x=\"43.78125\" y=\"113.019476\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_8\">\n      <!-- 1.0 -->\n      <g transform=\"translate(20.878125 116.818695) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-2e\" d=\"M 684 794 \nL 1344 794 \nL 1344 0 \nL 684 0 \nL 684 794 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_2\">\n     <g id=\"line2d_15\">\n      <path d=\"M 43.78125 74.852691 \nL 239.08125 74.852691 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_16\">\n      <g>\n       <use xlink:href=\"#m6c6813ab19\" x=\"43.78125\" y=\"74.852691\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_9\">\n      <!-- 1.5 -->\n      <g transform=\"translate(20.878125 78.65191) scale(0.1 -0.1)\">\n       <defs>\n        <path id=\"DejaVuSans-35\" d=\"M 691 4666 \nL 3169 4666 \nL 3169 4134 \nL 1269 4134 \nL 1269 2991 \nQ 1406 3038 1543 3061 \nQ 1681 3084 1819 3084 \nQ 2600 3084 3056 2656 \nQ 3513 2228 3513 1497 \nQ 3513 744 3044 326 \nQ 2575 -91 1722 -91 \nQ 1428 -91 1123 -41 \nQ 819 9 494 109 \nL 494 744 \nQ 775 591 1075 516 \nQ 1375 441 1709 441 \nQ 2250 441 2565 725 \nQ 2881 1009 2881 1497 \nQ 2881 1984 2565 2268 \nQ 2250 2553 1709 2553 \nQ 1456 2553 1204 2497 \nQ 953 2441 691 2322 \nL 691 4666 \nz\n\" transform=\"scale(0.015625)\"/>\n       </defs>\n       <use xlink:href=\"#DejaVuSans-31\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-35\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"ytick_3\">\n     <g id=\"line2d_17\">\n      <path d=\"M 43.78125 36.685906 \nL 239.08125 36.685906 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #b0b0b0; stroke-width: 0.8; stroke-linecap: square\"/>\n     </g>\n     <g id=\"line2d_18\">\n      <g>\n       <use xlink:href=\"#m6c6813ab19\" x=\"43.78125\" y=\"36.685906\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n      </g>\n     </g>\n     <g id=\"text_10\">\n      <!-- 2.0 -->\n      <g transform=\"translate(20.878125 40.485125) scale(0.1 -0.1)\">\n       <use xlink:href=\"#DejaVuSans-32\"/>\n       <use xlink:href=\"#DejaVuSans-2e\" transform=\"translate(63.623047 0)\"/>\n       <use xlink:href=\"#DejaVuSans-30\" transform=\"translate(95.410156 0)\"/>\n      </g>\n     </g>\n    </g>\n    <g id=\"text_11\">\n     <!-- loss -->\n     <g transform=\"translate(14.798437 86.157813) rotate(-90) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-6c\" d=\"M 603 4863 \nL 1178 4863 \nL 1178 0 \nL 603 0 \nL 603 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-6f\" d=\"M 1959 3097 \nQ 1497 3097 1228 2736 \nQ 959 2375 959 1747 \nQ 959 1119 1226 758 \nQ 1494 397 1959 397 \nQ 2419 397 2687 759 \nQ 2956 1122 2956 1747 \nQ 2956 2369 2687 2733 \nQ 2419 3097 1959 3097 \nz\nM 1959 3584 \nQ 2709 3584 3137 3096 \nQ 3566 2609 3566 1747 \nQ 3566 888 3137 398 \nQ 2709 -91 1959 -91 \nQ 1206 -91 779 398 \nQ 353 888 353 1747 \nQ 353 2609 779 3096 \nQ 1206 3584 1959 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-6c\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(27.783203 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(88.964844 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(141.064453 0)\"/>\n     </g>\n    </g>\n   </g>\n   <g id=\"line2d_19\">\n    <path d=\"M 52.658523 13.501911 \nL 54.451911 17.630868 \nL 56.2453 22.073655 \nL 58.038688 25.002439 \nL 59.832076 28.277301 \nL 61.625465 32.085677 \nL 63.418853 39.041529 \nL 65.212242 40.785481 \nL 67.00563 46.673586 \nL 68.799019 45.446578 \nL 70.592407 52.729381 \nL 72.385795 54.318684 \nL 74.179184 59.348108 \nL 75.972572 63.80661 \nL 77.765961 68.681995 \nL 79.559349 68.226011 \nL 81.352738 73.934216 \nL 83.146126 77.450292 \nL 84.939514 84.650416 \nL 86.732903 83.387172 \nL 88.526291 82.959378 \nL 90.31968 84.509662 \nL 92.113068 90.114213 \nL 93.906457 91.286551 \nL 95.699845 95.523194 \nL 97.493233 90.300092 \nL 99.286622 95.143637 \nL 101.08001 100.108973 \nL 102.873399 98.307065 \nL 104.666787 98.264806 \nL 106.460176 101.632567 \nL 108.253564 104.277996 \nL 110.046952 104.477069 \nL 111.840341 104.589041 \nL 113.633729 103.281136 \nL 115.427118 105.918066 \nL 117.220506 112.841132 \nL 119.013895 106.454664 \nL 120.807283 106.143255 \nL 122.600671 113.639937 \nL 124.39406 109.60619 \nL 126.187448 117.612048 \nL 127.980837 114.350576 \nL 129.774225 117.118996 \nL 131.567614 113.090622 \nL 133.361002 112.158083 \nL 135.15439 108.952988 \nL 136.947779 114.93588 \nL 138.741167 120.427077 \nL 140.534556 120.217625 \nL 142.327944 124.813509 \nL 144.121333 121.435261 \nL 145.914721 117.378801 \nL 147.70811 121.156461 \nL 149.501498 119.283639 \nL 151.294886 119.60211 \nL 153.088275 126.028788 \nL 154.881663 122.36654 \nL 156.675052 119.892321 \nL 158.46844 120.664496 \nL 160.261829 119.87843 \nL 162.055217 126.715272 \nL 163.848605 122.116048 \nL 165.641994 128.449137 \nL 167.435382 126.883207 \nL 169.228771 119.943989 \nL 171.022159 121.40808 \nL 172.815548 119.947792 \nL 174.608936 120.263842 \nL 176.402324 126.133371 \nL 178.195713 120.171217 \nL 179.989101 126.933059 \nL 181.78249 128.019205 \nL 183.575878 125.224773 \nL 185.369267 123.504253 \nL 187.162655 124.946706 \nL 188.956043 130.042658 \nL 190.749432 122.334117 \nL 192.54282 124.294114 \nL 194.336209 123.858481 \nL 196.129597 124.666477 \nL 197.922986 130.006314 \nL 199.716374 136.567372 \nL 201.509762 124.567299 \nL 203.303151 134.65054 \nL 205.096539 125.694134 \nL 206.889928 123.156728 \nL 208.683316 131.082477 \nL 210.476705 128.385767 \nL 212.270093 128.334745 \nL 214.063481 132.05651 \nL 215.85687 132.366682 \nL 217.650258 126.579637 \nL 219.443647 127.784361 \nL 221.237035 132.054126 \nL 223.030424 129.327032 \nL 224.823812 129.824962 \nL 226.6172 139.45952 \nL 228.410589 138.949274 \nL 230.203977 127.153399 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n   </g>\n   <g id=\"line2d_20\">\n    <path d=\"M 52.658523 13.5 \nL 54.451911 17.640478 \nL 56.2453 22.089434 \nL 58.038688 25.036089 \nL 59.832076 28.316739 \nL 61.625465 32.126007 \nL 63.418853 39.101268 \nL 65.212242 40.844765 \nL 67.00563 46.758013 \nL 68.799019 45.516536 \nL 70.592407 52.82015 \nL 72.385795 54.373937 \nL 74.179184 59.426265 \nL 75.972572 63.881664 \nL 77.765961 68.759643 \nL 79.559349 68.297916 \nL 81.352738 73.99563 \nL 83.146126 77.542244 \nL 84.939514 84.742377 \nL 86.732903 83.462026 \nL 88.526291 83.057682 \nL 90.31968 84.599721 \nL 92.113068 90.191041 \nL 93.906457 91.351586 \nL 95.699845 95.595909 \nL 97.493233 90.346928 \nL 99.286622 95.201629 \nL 101.08001 100.170541 \nL 102.873399 98.376495 \nL 104.666787 98.332644 \nL 106.460176 101.694581 \nL 108.253564 104.345852 \nL 110.046952 104.518846 \nL 111.840341 104.609306 \nL 113.633729 103.318672 \nL 115.427118 105.965921 \nL 117.220506 112.895275 \nL 119.013895 106.491382 \nL 120.807283 106.181656 \nL 122.600671 113.705478 \nL 124.39406 109.653126 \nL 126.187448 117.641322 \nL 127.980837 114.390792 \nL 129.774225 117.14539 \nL 131.567614 113.13946 \nL 133.361002 112.205884 \nL 135.15439 108.977493 \nL 136.947779 114.974863 \nL 138.741167 120.465914 \nL 140.534556 120.256954 \nL 142.327944 124.876033 \nL 144.121333 121.469389 \nL 145.914721 117.435487 \nL 147.70811 121.216869 \nL 149.501498 119.313332 \nL 151.294886 119.663473 \nL 153.088275 126.055992 \nL 154.881663 122.423949 \nL 156.675052 119.899805 \nL 158.46844 120.670761 \nL 160.261829 119.877989 \nL 162.055217 126.758364 \nL 163.848605 122.169482 \nL 165.641994 128.471604 \nL 167.435382 126.906588 \nL 169.228771 119.948916 \nL 171.022159 121.402543 \nL 172.815548 119.942128 \nL 174.608936 120.292301 \nL 176.402324 126.168951 \nL 178.195713 120.21661 \nL 179.989101 126.948246 \nL 181.78249 128.050831 \nL 183.575878 125.217676 \nL 185.369267 123.445278 \nL 187.162655 124.905848 \nL 188.956043 130.08296 \nL 190.749432 122.324754 \nL 192.54282 124.276547 \nL 194.336209 123.89896 \nL 196.129597 124.657213 \nL 197.922986 130.042944 \nL 199.716374 136.592146 \nL 201.509762 124.612957 \nL 203.303151 134.713573 \nL 205.096539 125.78636 \nL 206.889928 123.301371 \nL 208.683316 131.138481 \nL 210.476705 128.429864 \nL 212.270093 128.383565 \nL 214.063481 132.116172 \nL 215.85687 132.30535 \nL 217.650258 126.327358 \nL 219.443647 127.66263 \nL 221.237035 132.112705 \nL 223.030424 129.354263 \nL 224.823812 129.824161 \nL 226.6172 139.5 \nL 228.410589 138.96675 \nL 230.203977 127.153116 \n\" clip-path=\"url(#p6f0cdbcb73)\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n   </g>\n   <g id=\"patch_3\">\n    <path d=\"M 43.78125 145.8 \nL 43.78125 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_4\">\n    <path d=\"M 239.08125 145.8 \nL 239.08125 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_5\">\n    <path d=\"M 43.78125 145.8 \nL 239.08125 145.8 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"patch_6\">\n    <path d=\"M 43.78125 7.2 \nL 239.08125 7.2 \n\" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n   </g>\n   <g id=\"legend_1\">\n    <g id=\"patch_7\">\n     <path d=\"M 131.048438 44.55625 \nL 232.08125 44.55625 \nQ 234.08125 44.55625 234.08125 42.55625 \nL 234.08125 14.2 \nQ 234.08125 12.2 232.08125 12.2 \nL 131.048438 12.2 \nQ 129.048438 12.2 129.048438 14.2 \nL 129.048438 42.55625 \nQ 129.048438 44.55625 131.048438 44.55625 \nz\n\" style=\"fill: #ffffff; opacity: 0.8; stroke: #cccccc; stroke-linejoin: miter\"/>\n    </g>\n    <g id=\"line2d_21\">\n     <path d=\"M 133.048438 20.298438 \nL 143.048438 20.298438 \nL 153.048438 20.298438 \n\" style=\"fill: none; stroke: #1f77b4; stroke-width: 1.5; stroke-linecap: square\"/>\n    </g>\n    <g id=\"text_12\">\n     <!-- fp32 -->\n     <g transform=\"translate(161.048438 23.798438) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-66\" d=\"M 2375 4863 \nL 2375 4384 \nL 1825 4384 \nQ 1516 4384 1395 4259 \nQ 1275 4134 1275 3809 \nL 1275 3500 \nL 2222 3500 \nL 2222 3053 \nL 1275 3053 \nL 1275 0 \nL 697 0 \nL 697 3053 \nL 147 3053 \nL 147 3500 \nL 697 3500 \nL 697 3744 \nQ 697 4328 969 4595 \nQ 1241 4863 1831 4863 \nL 2375 4863 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-33\" d=\"M 2597 2516 \nQ 3050 2419 3304 2112 \nQ 3559 1806 3559 1356 \nQ 3559 666 3084 287 \nQ 2609 -91 1734 -91 \nQ 1441 -91 1130 -33 \nQ 819 25 488 141 \nL 488 750 \nQ 750 597 1062 519 \nQ 1375 441 1716 441 \nQ 2309 441 2620 675 \nQ 2931 909 2931 1356 \nQ 2931 1769 2642 2001 \nQ 2353 2234 1838 2234 \nL 1294 2234 \nL 1294 2753 \nL 1863 2753 \nQ 2328 2753 2575 2939 \nQ 2822 3125 2822 3475 \nQ 2822 3834 2567 4026 \nQ 2313 4219 1838 4219 \nQ 1578 4219 1281 4162 \nQ 984 4106 628 3988 \nL 628 4550 \nQ 988 4650 1302 4700 \nQ 1616 4750 1894 4750 \nQ 2613 4750 3031 4423 \nQ 3450 4097 3450 3541 \nQ 3450 3153 3228 2886 \nQ 3006 2619 2597 2516 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-66\"/>\n      <use xlink:href=\"#DejaVuSans-70\" transform=\"translate(35.205078 0)\"/>\n      <use xlink:href=\"#DejaVuSans-33\" transform=\"translate(98.681641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-32\" transform=\"translate(162.304688 0)\"/>\n     </g>\n    </g>\n    <g id=\"line2d_22\">\n     <path d=\"M 133.048438 34.976562 \nL 143.048438 34.976562 \nL 153.048438 34.976562 \n\" style=\"fill: none; stroke-dasharray: 5.55,2.4; stroke-dashoffset: 0; stroke: #bf00bf; stroke-width: 1.5\"/>\n    </g>\n    <g id=\"text_13\">\n     <!-- bf16 autocast -->\n     <g transform=\"translate(161.048438 38.476562) scale(0.1 -0.1)\">\n      <defs>\n       <path id=\"DejaVuSans-62\" d=\"M 3116 1747 \nQ 3116 2381 2855 2742 \nQ 2594 3103 2138 3103 \nQ 1681 3103 1420 2742 \nQ 1159 2381 1159 1747 \nQ 1159 1113 1420 752 \nQ 1681 391 2138 391 \nQ 2594 391 2855 752 \nQ 3116 1113 3116 1747 \nz\nM 1159 2969 \nQ 1341 3281 1617 3432 \nQ 1894 3584 2278 3584 \nQ 2916 3584 3314 3078 \nQ 3713 2572 3713 1747 \nQ 3713 922 3314 415 \nQ 2916 -91 2278 -91 \nQ 1894 -91 1617 61 \nQ 1341 213 1159 525 \nL 1159 0 \nL 581 0 \nL 581 4863 \nL 1159 4863 \nL 1159 2969 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-61\" d=\"M 2194 1759 \nQ 1497 1759 1228 1600 \nQ 959 1441 959 1056 \nQ 959 750 1161 570 \nQ 1363 391 1709 391 \nQ 2188 391 2477 730 \nQ 2766 1069 2766 1631 \nL 2766 1759 \nL 2194 1759 \nz\nM 3341 1997 \nL 3341 0 \nL 2766 0 \nL 2766 531 \nQ 2569 213 2275 61 \nQ 1981 -91 1556 -91 \nQ 1019 -91 701 211 \nQ 384 513 384 1019 \nQ 384 1609 779 1909 \nQ 1175 2209 1959 2209 \nL 2766 2209 \nL 2766 2266 \nQ 2766 2663 2505 2880 \nQ 2244 3097 1772 3097 \nQ 1472 3097 1187 3025 \nQ 903 2953 641 2809 \nL 641 3341 \nQ 956 3463 1253 3523 \nQ 1550 3584 1831 3584 \nQ 2591 3584 2966 3190 \nQ 3341 2797 3341 1997 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-75\" d=\"M 544 1381 \nL 544 3500 \nL 1119 3500 \nL 1119 1403 \nQ 1119 906 1312 657 \nQ 1506 409 1894 409 \nQ 2359 409 2629 706 \nQ 2900 1003 2900 1516 \nL 2900 3500 \nL 3475 3500 \nL 3475 0 \nL 2900 0 \nL 2900 538 \nQ 2691 219 2414 64 \nQ 2138 -91 1772 -91 \nQ 1169 -91 856 284 \nQ 544 659 544 1381 \nz\nM 1991 3584 \nL 1991 3584 \nz\n\" transform=\"scale(0.015625)\"/>\n       <path id=\"DejaVuSans-63\" d=\"M 3122 3366 \nL 3122 2828 \nQ 2878 2963 2633 3030 \nQ 2388 3097 2138 3097 \nQ 1578 3097 1268 2742 \nQ 959 2388 959 1747 \nQ 959 1106 1268 751 \nQ 1578 397 2138 397 \nQ 2388 397 2633 464 \nQ 2878 531 3122 666 \nL 3122 134 \nQ 2881 22 2623 -34 \nQ 2366 -91 2075 -91 \nQ 1284 -91 818 406 \nQ 353 903 353 1747 \nQ 353 2603 823 3093 \nQ 1294 3584 2113 3584 \nQ 2378 3584 2631 3529 \nQ 2884 3475 3122 3366 \nz\n\" transform=\"scale(0.015625)\"/>\n      </defs>\n      <use xlink:href=\"#DejaVuSans-62\"/>\n      <use xlink:href=\"#DejaVuSans-66\" transform=\"translate(63.476562 0)\"/>\n      <use xlink:href=\"#DejaVuSans-31\" transform=\"translate(98.681641 0)\"/>\n      <use xlink:href=\"#DejaVuSans-36\" transform=\"translate(162.304688 0)\"/>\n      <use xlink:href=\"#DejaVuSans-20\" transform=\"translate(225.927734 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(257.714844 0)\"/>\n      <use xlink:href=\"#DejaVuSans-75\" transform=\"translate(318.994141 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(382.373047 0)\"/>\n      <use xlink:href=\"#DejaVuSans-6f\" transform=\"translate(421.582031 0)\"/>\n      <use xlink:href=\"#DejaVuSans-63\" transform=\"translate(482.763672 0)\"/>\n      <use xlink:href=\"#DejaVuSans-61\" transform=\"translate(537.744141 0)\"/>\n      <use xlink:href=\"#DejaVuSans-73\" transform=\"translate(599.023438 0)\"/>\n      <use xlink:href=\"#DejaVuSans-74\" transform=\"translate(651.123047 0)\"/>\n     </g>\n    </g>\n   </g>\n  </g>\n </g>\n <defs>\n  <clipPath id=\"p6f0cdbcb73\">\n   <rect x=\"43.78125\" y=\"7.2\" width=\"195.3\" height=\"138.6\"/>\n  </clipPath>\n </defs>\n</svg>\n"
     },
     "metadata": {}
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-15 fw=all -->\n",
    "\n",
    "The two curves lie on top of each other and their validation accuracies agree:\n",
    "16-bit rounding perturbs each step slightly, so the trajectories are not\n",
    "bitwise identical, but they descend at the same rate to the same place. The\n",
    "reported time reflects this CPU-sized experiment: bf16\n",
    "may be no faster, and can be slower when casting overhead dominates. The\n",
    "wall-clock payoff appears on accelerators with native 16-bit matrix units and\n",
    "in models large enough to occupy them; speedup depends on hardware, shapes,\n",
    "and the input pipeline (we turn to GPUs in that section). Activations\n",
    "still occupy half the memory. Note what mixed\n",
    "precision does *not* buy: the master weights and any Adam state remain fp32,\n",
    "so the parameter and optimizer terms in the memory arithmetic of\n",
    "that section do not shrink. The savings are in activations\n",
    "and speed.\n",
    "\n",
    "\n",
    "### Loss Scaling for fp16\n",
    "\n",
    "With bf16, the recipe above is complete. fp16 has one more failure mode, and\n",
    "it is the opposite end of the axis from the overflow that opened this section:\n",
    "*gradient underflow*. Many gradients are small. Below about\n",
    "$6.10 \\times 10^{-5}$ fp16 uses increasingly coarse subnormals, and below\n",
    "about $5.96 \\times 10^{-8}$ values round to zero:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-loss-scaling-for-fp16-1"
   },
   "source": [
    "g = torch.tensor(1e-8)\n",
    "g.half(), (g * 2**16).half()"
   ],
   "execution_count": 12,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(tensor(0., dtype=torch.float16), tensor(0.0007, dtype=torch.float16))"
     },
     "metadata": {},
     "execution_count": 12
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-16 fw=all -->\n",
    "\n",
    "The gradient vanishes, yet the same value scaled by $2^{16}$ is perfectly\n",
    "representable. That is the idea of *loss scaling*: multiply the loss by a\n",
    "large constant before backpropagation, so that by linearity every gradient is\n",
    "scaled into representable territory, then divide the gradients by the same\n",
    "constant before the optimizer step."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-17 fw=pytorch -->\n",
    "\n",
    "`torch.amp.GradScaler` automates it,\n",
    "choosing the scale dynamically: start high, shrink when scaled gradients\n",
    "overflow to `inf` (skipping that step), grow back periodically."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-loss-scaling-for-fp16-2"
   },
   "source": [
    "torch.manual_seed(0)\n",
    "net = nn.Sequential(nn.Flatten(), nn.Linear(784, 256), nn.ReLU(),\n",
    "                    nn.Linear(256, 10))\n",
    "opt = torch.optim.SGD(net.parameters(), lr=0.1)\n",
    "scaler = torch.amp.GradScaler('cpu')\n",
    "for X, y in batches[:20]:\n",
    "    with torch.autocast('cpu', dtype=torch.float16):\n",
    "        loss = F.cross_entropy(net(X), y)\n",
    "    scaler.scale(loss).backward()  # gradients of (scale * loss)\n",
    "    scaler.step(opt)               # unscale, check for inf/nan, then step\n",
    "    scaler.update()\n",
    "    opt.zero_grad()\n",
    "print(f'loss {loss.item():.3f}, loss scale {scaler.get_scale():.0f}')"
   ],
   "execution_count": 13,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "loss 1.388, loss scale 65536\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-18 fw=pytorch -->\n",
    "\n",
    "Keep the two failure modes straight: `GradScaler` exists to prevent gradient\n",
    "*underflow*; its `inf`/NaN check handles overflow as a side effect by skipping\n",
    "the bad step. bf16 normally needs no scaler, because its normal exponent range\n",
    "matches fp32 and avoids fp16's narrow-range failure. Hence the modern default:\n",
    "on hardware with bf16 support (Ampere and later GPUs, TPUs, recent CPUs), use\n",
    "bf16 autocast and stop there; reach for fp16 plus `GradScaler` only on older\n",
    "accelerators."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-19 fw=all -->\n",
    "\n",
    "## When Numerics Bite\n",
    "\n",
    "A short field guide for the day training misbehaves.\n",
    "\n",
    "**The loss is NaN.** NaN is usually a symptom, not the disease; the disease is\n",
    "`inf`, because `inf` arithmetic breeds NaN:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-when-numerics-bite-1"
   },
   "source": [
    "s = torch.tensor(60000., dtype=torch.float16) * 2  # overflows\n",
    "s, s - s"
   ],
   "execution_count": 14,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(tensor(inf, dtype=torch.float16), tensor(nan, dtype=torch.float16))"
     },
     "metadata": {},
     "execution_count": 14
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-20 fw=all -->\n",
    "\n",
    "By the time a NaN reaches your loss, the overflow that spawned it may be many\n",
    "operations upstream, and once a NaN lands in the weights it poisons every\n",
    "subsequent step. So diagnose in order: first check ranges (is anything in\n",
    "fp16? are intermediate values in the $10^4$ regime and headed for the 65504\n",
    "ceiling?), and only then blame the learning rate."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-21 fw=pytorch -->\n",
    "\n",
    "Under autocast with\n",
    "`GradScaler`, the gradient check catches the `inf` in the very step it occurs\n",
    "and skips the update, one more reason to prefer the standard recipe over\n",
    "hand-rolled fp16 even while debugging."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-22 fw=all -->\n",
    "\n",
    "**Let the library take the log.** The naive evaluation of\n",
    "$\\log \\sum_i \\exp(x_i)$ overflows long before the answer does:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-when-numerics-bite-2"
   },
   "source": [
    "x = torch.tensor([12.0, 11.0, 10.0], dtype=torch.float16)\n",
    "x.exp().sum().log(), torch.logsumexp(x, dim=0)"
   ],
   "execution_count": 15,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(tensor(inf, dtype=torch.float16), tensor(12.4062, dtype=torch.float16))"
     },
     "metadata": {},
     "execution_count": 15
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-23 fw=all -->\n",
    "\n",
    "The answer, 12.4, is unremarkable; only the intermediate $e^{12}$ exceeds\n",
    "65504. The subtract-the-max shift from that section fixes\n",
    "it, and the practical form of the lesson is to never hand-roll the pattern."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-24 fw=pytorch -->\n",
    "\n",
    "`torch.logsumexp`, `F.log_softmax`, and `F.cross_entropy` all build the shift\n",
    "in."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-25 fw=all -->\n",
    "\n",
    "**Accumulate in fp32.** Long sums in a 16-bit dtype drift, because once the\n",
    "running total is large, each small increment falls below the spacing of\n",
    "representable values and is partly or wholly rounded away:"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "numerics-when-numerics-bite-3"
   },
   "source": [
    "x = torch.full((10**7,), 1e-3, dtype=torch.float16)\n",
    "x.cumsum(0)[-1], x.cumsum(0, dtype=torch.float32)[-1]"
   ],
   "execution_count": 16,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(tensor(9784., dtype=torch.float16), tensor(10004.0439))"
     },
     "metadata": {},
     "execution_count": 16
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-26 fw=pytorch -->\n",
    "\n",
    "The fp16 running total is short by about 2 percent; keeping the *accumulator*\n",
    "in fp32 while the data stays fp16 recovers the exact answer. Means over large\n",
    "batches, epoch-level loss totals, and variance computations all follow this\n",
    "pattern, and it is precisely why autocast pins reductions and normalizations\n",
    "to fp32. When you write your own, pass `dtype=torch.float32` to the reduction."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-27 fw=all -->\n",
    "\n",
    "Finally, do not expect bitwise equality across\n",
    "numeric configurations: tf32 versus fp32, or mixed precision on versus off,\n",
    "differ in the last bits by design. What reproducibility you can demand, and\n",
    "how to get it, is the subject of that section.\n",
    "\n",
    "## Summary\n",
    "\n",
    "A dtype is a contract about range and precision, and the 16-bit formats split\n",
    "the difference between them: fp16 keeps precision and forfeits range, bf16\n",
    "keeps fp32's range and forfeits precision, which suits deep learning better."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-28 fw=pytorch -->\n",
    "\n",
    "Casting a model (`net.bfloat16()`) converts its parameters and is the tool\n",
    "for inference; training instead uses autocast, which keeps fp32 master\n",
    "weights and runs matrix multiplications in 16 bits under a per-operation\n",
    "policy. fp16 training additionally needs `GradScaler` to stop small gradients\n",
    "from underflowing to zero; bf16 does not."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- d2l:prose id=numerics-md-29 fw=all -->\n",
    "\n",
    "When numbers misbehave: check for\n",
    "overflow before blaming the learning rate, use the library's `logsumexp`\n",
    "family, and accumulate long sums in fp32.\n",
    "\n",
    "## Exercises\n",
    "\n",
    "1. Redo the memory arithmetic of that section for\n",
    "   mixed-precision training with Adam: fp32 master weights, fp32 gradients,\n",
    "   two fp32 moment estimates, and bf16 activations. Which term dominates now,\n",
    "   and how does the total compare with all-fp32 training?\n",
    "1. Time the fp32 and 16-bit runs of `train` against each other while\n",
    "   shrinking the hidden width and the batch size. Find a model small enough\n",
    "   that mixed precision is *slower* than fp32, and explain where the\n",
    "   crossover comes from.\n",
    "1. Print every field of `torch.finfo(torch.float8_e4m3fn)` (in JAX,\n",
    "   `jnp.finfo(jnp.float8_e4m3fn)`; in TensorFlow,\n",
    "   `ml_dtypes.finfo(ml_dtypes.float8_e4m3fn)`; MXNet has no fp8 dtype, so\n",
    "   borrow the standalone `ml_dtypes` package) and compare with fp16 and\n",
    "   bf16. Explain\n",
    "   the name `e4m3fn`, including why its `max` is 448 rather than the value\n",
    "   the exponent bits alone would suggest.\n",
    "1. Under autocast, normalization layers run in fp32. To see why, take the\n",
    "   `RMSNorm` layer of that section, feed it inputs with a\n",
    "   standard deviation of about 100, and force the computation to fp16. Which\n",
    "   intermediate quantity fails first?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "d2l-pytorch",
   "display_name": "d2l (PyTorch)",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
