<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://thomaszhang2661.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://thomaszhang2661.github.io/" rel="alternate" type="text/html" /><updated>2026-12-01T06:25:16+00:00</updated><id>https://thomaszhang2661.github.io/feed.xml</id><title type="html">Jian (Thomas) Zhang</title><subtitle>Notes on LLM inference, KV-cache quantization and GPU kernels.</subtitle><author><name>Jian (Thomas) Zhang</name></author><entry xml:lang="zh-CN"><title type="html">在 Hopper 上开 4-bit KV cache，decode 可能比 FP8 还慢：原因和解法</title><link href="https://thomaszhang2661.github.io/2026/11/30/hopper-4bit-kv-cache-zh/" rel="alternate" type="text/html" title="在 Hopper 上开 4-bit KV cache，decode 可能比 FP8 还慢：原因和解法" /><published>2026-11-30T00:00:00+00:00</published><updated>2026-11-30T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/30/hopper-4bit-kv-cache-zh</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/30/hopper-4bit-kv-cache-zh/"><![CDATA[<p><a href="/2026/11/30/hopper-4bit-kv-cache/">English version</a></p>

<p>如果你在 H100 或 H200 上用 vLLM 0.33 部署模型，为了多放一些 token 打开了它的两种 4-bit KV cache 之一（<code class="language-plaintext highlighter-rouge">nvfp4</code>，由 FlashInfer 解码；或 <code class="language-plaintext highlighter-rouge">ultraquant_4bit</code>，由 Triton 解码），那你很可能在 decode 速度上付出了代价。Hopper 没有 FP4 硬件，每个 4-bit 值都要在 attention kernel 里用软件解包、乘上缩放因子，而 H200 留给这件事的空间非常小。这篇文章讲时间花在了哪里，以及能解决这个问题的 kernel 长什么样。代码（Apache-2.0）和完整报告的链接在文末。</p>

<h2 id="一张表看清问题">一张表看清问题</h2>

<p>下面是 H200 上 batch 32、上下文 16K 时，各个 decode kernel 调用一次的情况。一次调用计算的是<strong>全部 32 条序列在一个 decode 步里的一层 attention</strong>：32 个新 token 各自和自己之前的 16,384 个 token 做 attention，层的形状取 Llama-3.1-8B（8 个 KV head，head 维度 128）。字节数是 kernel 必须从 KV cache 读出的量，除以 H200 的 4.8 TB/s，就是这次调用理论上最快能多快。</p>

<table>
  <thead>
    <tr>
      <th>Kernel</th>
      <th>每次调用读取的 KV 字节</th>
      <th>满带宽下的时间</th>
      <th>实测</th>
      <th>用到的带宽比例</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>BF16（FlashInfer）</td>
      <td>32 × 8 × 16,384 × 2 × 256 B ≈ 2.15 GB</td>
      <td>0.447 ms</td>
      <td>0.495 ms</td>
      <td>约 90%</td>
    </tr>
    <tr>
      <td>FP8（XQA，vLLM 用的 kernel）</td>
      <td>≈ 1.07 GB</td>
      <td>0.224 ms</td>
      <td>0.255 ms</td>
      <td>约 88%</td>
    </tr>
    <tr>
      <td>NVFP4（FlashInfer，vLLM 的 4-bit 路径）</td>
      <td>每行 72 B，≈ 0.60 GB</td>
      <td>0.126 ms</td>
      <td>0.669 ms</td>
      <td>约 19%</td>
    </tr>
  </tbody>
</table>

<p>（因子 2 指 K 和 V。BF16 和 NVFP4 在同一个作业里计时，XQA 在另一个作业里。）</p>

<p>NVFP4 读的字节不到 BF16 的三分之一，按理应该快三倍以上，实际却更慢。BF16 和 FP8 都跑在接近内存带宽上限的位置，NVFP4 连五分之一都没用到。本文剩下的部分就是讲这些时间去了哪里。</p>

<p>一个完整的 decode 步要把上面的时间乘以层数（Llama-3.1-8B 是 32 层），再加上读权重和其他矩阵乘。在 vLLM 里，同样的 32 × 16K 形状，FP8 每步 12.66 ms，其中 attention 大约是 32 × 0.255 ≈ 8 ms。这也是为什么我们的 kernel 在这个形状上比 XQA 快 1.37×，整步却只快 1.16×（10.94 ms）：attention 只是一步中的一部分，而且我们的方案还多了一次 query 旋转和一次 sink 合并。</p>

<h2 id="先检查这三件事">先检查这三件事</h2>

<p>以下来自 H200、vLLM 0.33 上的测量，未特别说明时模型是 Llama-3.1-8B。</p>

<ol>
  <li><strong>在 Hopper 上，vLLM 的 NVFP4 KV cache 比 FP8 慢。</strong> <code class="language-plaintext highlighter-rouge">--kv-cache-dtype nvfp4</code> 省了显存，但在 batch 8 及以上时 decode 比 FP8 慢 1.4–2.3×，batch 1 时慢约 1.1×。vLLM 的另一个 4-bit 选项 UltraQuant 在 NVIDIA GPU 上会退回 Triton 的 decode kernel，每步比 FP8 慢 1.8–4.4×。</li>
  <li><strong>你的 FP8 基线可能不是快的那个。</strong> 用 FlashInfer 加 FP8 KV cache 时，vLLM 只有在访问 NVIDIA artifact 服务器的网络检查通过时，才会用 TensorRT-LLM 的 XQA decode kernel。在代理后面，这个检查可能失败，vLLM 就会不报错地退回 FlashInfer 自己的 decode kernel。在日志里找 <code class="language-plaintext highlighter-rouge">decode_backend=xqa</code>。</li>
  <li><strong>Qwen2 系列的推理模型在 FP8 KV 下可能静默崩坏。</strong> 把第 0 层保留为 BF16（<code class="language-plaintext highlighter-rouge">--kv-cache-dtype-skip-layers 0</code>，28 层的模型多用 1/28 的 KV 显存），在我们的测试里能把它们救回来。原因很具体：第 0 层的 key 向量带有一个很大的均值，它来自 key 投影的 bias 经过 RoPE 之后的结果，每个张量一个 FP8 缩放因子表示不好它。并不是所有带 key bias 的模型都会崩（R1-Distill-Qwen-14B 和 Qwen2.5-14B 就没有），而且我们从权重算出来的第 0 层统计量也预测不了哪些会崩，所以请在你自己的模型上分别测一下第 0 层用不用 BF16。</li>
</ol>

<h2 id="为什么软件解码-4-bit-在-hopper-上慢">为什么软件解码 4-bit 在 Hopper 上慢</h2>

<p>decode attention 本应是内存瓶颈：把 KV cache 读一遍，每个字节做一点点计算。4-bit cache 读的字节大约是 FP8 的一半，所以应该快将近一倍。问题出在指令发射速率上。</p>

<p>H200 每秒最多发射约 33 万亿条线程指令（132 个 SM × 4 个调度器 × 32 条 lane × 1.98 GHz），而带宽是 4.8 TB/s。所以一个跑满带宽的 kernel，每读一个字节只能花<strong>大约 7 条指令</strong>。H100 和 A100 约为 10，L40S 约为 53。在 A100 或 L40S 上划算的软件 4-bit 解码，到了 H200 上就可能卡在指令发射上：H200 的带宽增长比算力快。</p>

<p>我们用 NVBit 统计了实际执行的指令数（batch 32，16K 上下文）：</p>

<p><img src="/assets/hopper_issue_budget.png" alt="各 kernel 每字节指令数与 H200 预算的对比" /></p>

<p>能看出两个问题，快的 kernel 必须两个都解决：</p>

<ul>
  <li><strong>指令太多。</strong> vLLM 的 UltraQuant Triton kernel 每字节约执行 42 条指令，是预算的六倍，主要是整数、位运算、选择操作，外加按字节的加载。FlashInfer 的 NVFP4 decode 和我们自己的第一版 Triton kernel 执行的是预算的两到三倍（多出的整数和位运算、类型转换，Triton 里还有解码后的值在 shared memory 里来回搬运）。</li>
  <li><strong>在等待。</strong> 即便如此，这些 kernel 也只用到发射能力的 40–52%：它们还卡在串行的加载、解码和矩阵乘上。BitDecoding 的 INT4 Hopper kernel 指令数在预算以内，但 38% 的指令花在 barrier 上的自旋等待，而且把 4 个真实的 query head 补齐到 tensor core 要求的 64 行。在同一个作业里测 11 个形状，它在 batch 4 及以上只用到 22–26% 的带宽，每个形状都比更快的 FP8 kernel（XQA 或 FlashAttention-4）慢 1.12–1.83×。</li>
</ul>

<h2 id="解法长什么样">解法长什么样</h2>

<p>我们给 NVFP4、MXFP4 和 INT4 cache 写的 decode kernel 做了三个选择，外加一个便宜的位级解码：</p>

<ol>
  <li><strong>加载和计算重叠。</strong> 一个 producer warp 用 TMA（Hopper 的批量拷贝引擎）把 cache 拷进 shared memory 的槽位；两个计算 warpgroup 在下一批页面到达的同时做解码和乘法。</li>
  <li><strong>在写入时就排好 cache 的布局。</strong> cache 格式由我们决定，所以写 cache 的 kernel 会重排每一行 K 和每一块 V，让每个线程需要的 tensor core 片段落在连续的 16 字节块里：每个线程每行一次 128-bit 的 shared memory 加载，没有 bank 冲突，也不需要 shuffle。</li>
  <li><strong>交换矩阵操作数。</strong> 用 <code class="language-plaintext highlighter-rouge">wgmma.m64n8k16</code> 计算 S^T = K q^T 和 O^T = V^T P^T，把解码后的 K 和 V^T 放在寄存器操作数里，解码后的值就不用再写回 shared memory；人数很少的 query 组（4 或 8 个 head）放在宽度为 8 的那一维，不用补到 64 行。</li>
  <li><strong>用位放置做解码。</strong> 把 E2M1 的指数位和尾数位放到 FP16 的第 9–11 位、符号放到第 15 位，得到的正好是原值乘以 2^-14，次正规数也精确；每 16 个值一个的缩放因子用成对的半精度乘法来乘。</li>
</ol>

<p>每一步都有用。在一张 H200 上、batch 32、16K 上下文：只做流水线，带宽利用率是峰值的 24%；加上写入时的布局到 35%；解码后的操作数直接来自寄存器到 45%；再加上位级解码到 67%，此时每字节约 6.4 条指令，落在预算之内。</p>

<p>指令数已经在预算内，为什么还到不了 100%？在预算内是必要条件，不是充分条件。batch 8 及以上时，paged kernel 达到峰值带宽的 68–74%，而一个只读 cache 的 kernel 能到 93–95%，所以剩下的差距在解码的工作上，不在加载上。这时读取的字节数和任何一条流水线的吞吐都没有接近上限，而 kernel 中 57% 的指令用来重建和缩放 4-bit 值，每个值都要经过一串相互依赖的整数和半精度运算，才能交给 tensor core。指令延迟和这些依赖链很可能是剩下的瓶颈；我们没能证实，因为集群上拿不到 GPU 的 stall 计数器，也没有进一步优化。把 K 和 V 经过一次便宜的位放置后搬到 FP8 tensor core 上（报告里有介绍），能把这个比例提高到 79–89%。</p>

<h2 id="在-vllm-里能得到什么">在 vLLM 里能得到什么</h2>

<p>通过一个 vLLM 0.33 插件，配合一套精度方案（K 去均值、K 旋转、4 个 BF16 sink token、第 0 层用 BF16），和 vLLM 跑在 FlashInfer + XQA decode 上的 FP8 KV cache 对比：（方案的各个部分都不是新的：K 去均值沿用 SageAttention 的 “smooth K”，Hadamard 旋转来自 QuaRot，BF16 sink token 来自 StreamingLLM 和 IntactKV，敏感层保留高精度是常见做法；新的是 kernel）</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>我们的方案</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>KV cache 能放的 token 数</td>
      <td>1.63 倍</td>
    </tr>
    <tr>
      <td>decode 每步，Llama-3.1-8B，batch 8 及以上</td>
      <td>快 1.12–1.64×</td>
    </tr>
    <tr>
      <td>decode 每步，batch 1</td>
      <td>持平</td>
    </tr>
    <tr>
      <td>decode 每步，Qwen3-32B 和 Qwen2.5-72B</td>
      <td>快 1.04–1.18×（每步的时间主要花在读权重上）</td>
    </tr>
    <tr>
      <td>首 token 时间</td>
      <td>长 1.5–6%</td>
    </tr>
    <tr>
      <td>相对 BF16 KV 的精度（五个模型，重复运行）</td>
      <td>95% 置信区间排除了大于约 3.5 个点的损失</td>
    </tr>
    <tr>
      <td>120K token 时的精度（Llama-3.1-8B，多 key 检索）</td>
      <td>比 BF16 KV 低 15 个点（vLLM 默认的 FP8 路径：低 13 个点）</td>
    </tr>
    <tr>
      <td>单看 decode kernel，11 个形状</td>
      <td>比 XQA 和 FlashAttention-4 原生 FP8 中更快的那个还快 1.10–1.48×</td>
    </tr>
  </tbody>
</table>

<p><strong>有负载时的服务能力：两个不同的数字。</strong> 更大的 cache 在流量突增时最有用。从空 cache 开始，一张 H200 在 600 秒的突发里承受的推理类请求流（1K 输入、8K 输出 token）速率，是 FP8 的 1.23–1.32×（Llama-3.1-8B）、1.45–1.80×（Qwen3-32B）和 1.67–2.20×（Qwen2.5-72B）；在较高的速率下，FP8 的 cache 被占满、请求被抢占，而我们在 Llama-3.1-8B 上的 cache 占用始终不超过 81%。这些结果大多数点只有一个 seed。一张 GPU 能不能<em>持续</em>承受更高的速率，是另一个问题，我们没有得出定论。一个事先固定规则的稳态测试，在三个模型的计划速率上都没有显示出增益；这些计划速率取自突发测试的结果，结果对我们的配置来说定得太高了。看到结果后补测的速率给出的持续增益下界，在 Qwen3-32B 上约 1.31×，在 Qwen2.5-72B 上约 1.14×，在 Llama-3.1-8B 上的区间包含”没有增益”；因为这些速率是看过数据后选的，而且大多只有一个 seed，报告不声称有持续增益。如果你的流量是突发型的，第一个数字才是相关的；如果是平稳流量，请在自己的负载上实测。</p>

<p>不适用的场景：batch 1 的延迟，以及输入长、输出短的请求流，那时瓶颈是 prefill 而不是 cache。另外，在没有针对长上下文训练过的模型上，很长的上下文会损失精度：Llama-3.1-8B 在 120K token 时，我们的 cache 比 BF16 KV 低 15 个点。离线测试中，同样的题目上只做 FP8 存储只掉 3 个点，我们的 cache 掉 10 个点，所以在这个长度上，4-bit cache 会带来 FP8 存储没有的精度损失；在针对长上下文训练过的 Llama-3-8B-ProLong-512k 上，它和 BF16 的差距在 1.5 个点以内。</p>

<h2 id="试一试">试一试</h2>

<p>在 H100 或 H200 上的 vLLM 0.33.0 环境里：</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/thomaszhang2661/kv4-hopper <span class="o">&amp;&amp;</span> <span class="nb">cd </span>kv4-hopper
pip <span class="nb">install</span> <span class="nt">--no-deps</span> <span class="nt">-e</span> integrations/vllm_plugin
<span class="nb">export </span><span class="nv">PYTHONPATH</span><span class="o">=</span><span class="nv">$PWD</span>:<span class="nv">$PWD</span>/integrations/vllm_plugin <span class="nv">TORCH_CUDA_ARCH_LIST</span><span class="o">=</span>9.0a
<span class="nb">export </span><span class="nv">KV4_VLLM</span><span class="o">=</span>1 <span class="nv">KV4_CALIB</span><span class="o">=</span>results/calib/Llama-3.1-8B.pt <span class="nv">KV4_FORMAT</span><span class="o">=</span>nvfp4 <span class="nv">KV4_KMEAN</span><span class="o">=</span>1 <span class="nv">KV4_SINKS</span><span class="o">=</span>1 <span class="nv">KV4_BOUNDARY</span><span class="o">=</span>0
vllm serve meta-llama/Llama-3.1-8B <span class="nt">--kv-cache-dtype</span> ultraquant_4bit <span class="nt">--block-size</span> 64
</code></pre></div></div>

<p>报告中十个模型的校准文件在 <code class="language-plaintext highlighter-rouge">results/calib/</code>；其他模型可以用 <code class="language-plaintext highlighter-rouge">bench/calib_kv.py</code> 生成。想在自己的 GPU 上和 FP8 对比，<code class="language-plaintext highlighter-rouge">bench/vllm_forward_matrix.py</code> 会在同一个进程里给两种配置的纯 decode 步计时。</p>

<p>代码：<a href="https://github.com/thomaszhang2661/kv4-hopper">github.com/thomaszhang2661/kv4-hopper</a> · 报告：<a href="https://arxiv.org/abs/2611.30027v2">arXiv:2611.30027v2</a></p>

<p>上游：decode kernel 已在 FlashInfer 提交 PR <a href="https://github.com/flashinfer-ai/flashinfer/pull/10152">#10152</a>，vLLM 侧的接入方案见 RFC <a href="https://github.com/vllm-project/vllm/issues/70766">#70766</a>。</p>

<h2 id="局限">局限</h2>

<p>在没有针对长上下文训练过的模型上，长上下文会损失精度（见上）。只支持 head 维度 128、每个 KV head 最多 8 个 query head；不支持 MLA 模型、滑动窗口层和投机解码；vLLM 插件绑定 vLLM 0.33。kernel 和 vLLM 的数字都来自 H200；kernel 在 H100 上也能跑。在硬件支持 FP4 转换的 Blackwell 上，我们预计软件解包和原生 FP4 在小 batch 时 decode 差距不大，而在大 batch 和 prefill 时原生路径会胜出，但我们没有测过。</p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="kv-cache" /><category term="quantization" /><category term="vllm" /><category term="cuda" /><summary type="html"><![CDATA[H200 上 vLLM 的 4-bit KV cache decode 比 FP8 还慢：Hopper 没有 FP4 硬件，软件解包超出了每字节约 7 条指令的预算。围绕这个预算设计的 kernel，以及它在 vLLM 里带来的效果。]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://thomaszhang2661.github.io/assets/hopper_issue_budget.png" /><media:content medium="image" url="https://thomaszhang2661.github.io/assets/hopper_issue_budget.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Your 4-bit KV cache on Hopper is probably slower than FP8: why, and how to fix it</title><link href="https://thomaszhang2661.github.io/2026/11/30/hopper-4bit-kv-cache/" rel="alternate" type="text/html" title="Your 4-bit KV cache on Hopper is probably slower than FP8: why, and how to fix it" /><published>2026-11-30T00:00:00+00:00</published><updated>2026-11-30T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/30/hopper-4bit-kv-cache</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/30/hopper-4bit-kv-cache/"><![CDATA[<p><a href="/2026/11/30/hopper-4bit-kv-cache-zh/">中文版</a></p>

<p>If you serve LLMs with vLLM 0.33 on H100s or H200s and turned on one of its two 4-bit KV caches (<code class="language-plaintext highlighter-rouge">nvfp4</code>, decoded by
FlashInfer, or <code class="language-plaintext highlighter-rouge">ultraquant_4bit</code>, decoded by Triton) to fit more tokens, you probably paid for it in decode speed. Hopper has no FP4 hardware, so every 4-bit value has to be unpacked and scaled in software
inside the attention kernel, and on an H200 there is very little room for that. This post explains where the time
goes and what a kernel that fixes it looks like. Code (Apache-2.0) and the full report are linked at the end.</p>

<h2 id="the-problem-in-one-table">The problem in one table</h2>

<p>Here is one call of each decode kernel on an H200 at batch 32 and 16K context. One call computes <strong>one attention layer
for one decode step of all 32 sequences</strong>: each of the 32 new tokens attends to its own 16,384 earlier tokens, with
Llama-3.1-8B’s layer shape (8 KV heads, head dimension 128). The byte count is what the kernel must read from the
KV cache; dividing it by the H200’s 4.8 TB/s gives the fastest the call could possibly be.</p>

<table>
  <thead>
    <tr>
      <th>Kernel</th>
      <th>KV bytes read per call</th>
      <th>Time at full bandwidth</th>
      <th>Measured</th>
      <th>Share of bandwidth used</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>BF16 (FlashInfer)</td>
      <td>32 × 8 × 16,384 × 2 × 256 B ≈ 2.15 GB</td>
      <td>0.447 ms</td>
      <td>0.495 ms</td>
      <td>about 90%</td>
    </tr>
    <tr>
      <td>FP8 (XQA, the kernel vLLM uses)</td>
      <td>≈ 1.07 GB</td>
      <td>0.224 ms</td>
      <td>0.255 ms</td>
      <td>about 88%</td>
    </tr>
    <tr>
      <td>NVFP4 (FlashInfer, vLLM’s 4-bit path)</td>
      <td>72 B per row, ≈ 0.60 GB</td>
      <td>0.126 ms</td>
      <td>0.669 ms</td>
      <td>about 19%</td>
    </tr>
  </tbody>
</table>

<p>(The factor 2 is K and V. BF16 and NVFP4 were timed in one job, XQA in another.)</p>

<p>NVFP4 reads less than a third of BF16’s bytes, so it should be more than three times faster. It is slower. BF16 and
FP8 both run close to the memory limit; NVFP4 uses less than a fifth of it. The rest of this post is about where that
time goes.</p>

<p>A full decode step multiplies this by the number of layers (32 on Llama-3.1-8B) and adds the weight read and the other
matrix multiplies. In vLLM, the same 32 × 16K shape takes 12.66 ms per step with FP8, of which attention is roughly
32 × 0.255 ≈ 8 ms. That is also why our kernel, 1.37× faster than XQA at this shape, makes the whole step only 1.16× faster
(10.94 ms): attention is only part of the step, and the recipe adds a query rotation and a sink merge.</p>

<h2 id="three-things-to-check-first">Three things to check first</h2>

<p>These come from measurements on H200 with vLLM 0.33 and Llama-3.1-8B unless noted.</p>

<ol>
  <li><strong>vLLM’s NVFP4 KV cache is slower than FP8 on Hopper.</strong> <code class="language-plaintext highlighter-rouge">--kv-cache-dtype nvfp4</code> saves memory but decodes 1.4–2.3×
slower than FP8 at batch 8 and above, and about 1.1× slower at batch 1. vLLM’s other 4-bit option, UltraQuant,
falls back to Triton decode kernels on NVIDIA GPUs and is 1.8–4.4× slower than FP8 per step.</li>
  <li><strong>Your FP8 baseline may not be the fast one.</strong> With FlashInfer and an FP8 KV cache, vLLM uses TensorRT-LLM’s XQA
decode kernel only if a network check to NVIDIA’s artifact server succeeds. Behind a proxy that check can fail,
and vLLM falls back to FlashInfer’s own decode kernel without an error. Look for <code class="language-plaintext highlighter-rouge">decode_backend=xqa</code> in the log.</li>
  <li><strong>Qwen2-family reasoning models can collapse under FP8 KV, silently.</strong> Keeping layer 0 in BF16
(<code class="language-plaintext highlighter-rouge">--kv-cache-dtype-skip-layers 0</code>, 1/28 more KV memory on a 28-layer model) restored them in our runs. The reason
is specific: layer 0’s key vectors carry a large mean, which is the key projection’s bias seen through RoPE, and
a per-tensor FP8 scale cannot represent it well. Not every model with a key bias collapses (R1-Distill-Qwen-14B
and Qwen2.5-14B did not), and the layer-0 statistics we computed from the weights do not predict which ones do,
so test your own model with and without layer 0 in BF16.</li>
</ol>

<h2 id="why-software-4-bit-decoding-is-slow-on-hopper">Why software 4-bit decoding is slow on Hopper</h2>

<p>Decode attention is supposed to be memory-bound: read the KV cache once, do a little math per byte. A 4-bit cache
reads about half the bytes of FP8, so it should be almost twice as fast. The catch is the instruction issue rate.</p>

<p>An H200 issues at most about 33 trillion thread instructions per second (132 SMs × 4 schedulers × 32 lanes ×
1.98 GHz) and reads 4.8 TB/s. So a kernel that runs at full bandwidth can afford only <strong>about 7 instructions per
byte</strong> it reads. H100 and A100 have about 10, an L40S about 53. Software 4-bit decoding that pays off on an A100 or
L40S can therefore be issue-bound on an H200, whose bandwidth grew faster than its compute.</p>

<p>We counted executed instructions with NVBit (batch 32, 16K context):</p>

<p><img src="/assets/hopper_issue_budget.png" alt="Instructions per byte against the H200 budget" /></p>

<p>Two problems show up, and fast kernels need both fixed:</p>

<ul>
  <li><strong>Too many instructions.</strong> vLLM’s UltraQuant Triton kernel executes about 42 instructions per byte, six times the
budget, mostly integer, bit and select operations plus byte-wide loads. FlashInfer’s NVFP4 decode and our own first
Triton kernel execute two to three times the budget (extra integer and bit operations, conversions, and in Triton,
round trips of decoded values through shared memory).</li>
  <li><strong>Waiting.</strong> Even so, those kernels use only 40–52% of the issue rate: they also stall on serialized loading,
decoding and matrix multiplies. BitDecoding’s INT4 Hopper kernel stays within the budget but spends 38% of its
instructions spin-waiting on barriers and pads 4 real query heads to the tensor core’s 64 rows. Timed at 11 shapes
in one job, it reaches 22–26% of bandwidth at batch 4 and above and is 1.12–1.83× slower than the faster FP8
kernel (XQA or FlashAttention-4) at every shape.</li>
</ul>

<h2 id="what-the-fix-looks-like">What the fix looks like</h2>

<p>Our decode kernel for NVFP4, MXFP4 and INT4 caches makes three choices plus a cheap bit-level decode:</p>

<ol>
  <li><strong>Overlap loading with compute.</strong> One producer warp copies the cache with TMA (Hopper’s bulk-copy engine) into
shared-memory slots; two compute warpgroups decode and multiply while the next pages arrive.</li>
  <li><strong>Lay the cache out at write time.</strong> We control the cache format, so the cache-write kernel permutes each K row and
V block so that every thread’s tensor-core fragment sits in contiguous 16-byte chunks: one 128-bit shared-memory
load per thread per row, no bank conflicts, no shuffles.</li>
  <li><strong>Swap the matrix operands.</strong> Computing S^T = K q^T and O^T = V^T P^T with <code class="language-plaintext highlighter-rouge">wgmma.m64n8k16</code> puts the decoded K and
V^T in the register operand, so decoded values never go back to shared memory, and the small query group (4 or 8
heads) takes the 8-wide dimension instead of being padded to 64 rows.</li>
  <li><strong>Decode with bit placement.</strong> Placing E2M1’s exponent and mantissa bits at FP16 bits 9–11 and the sign at bit 15
gives the value times 2^-14 exactly, subnormal included; the per-16 scales are applied with paired half-precision
multiplies.</li>
</ol>

<p>Each step matters. On one H200 at batch 32 and 16K context, the pipeline alone reaches 24% of peak bandwidth; adding
the write-time layout gives 35%, decoded operands from registers 45%, and the bit-level decode 67%, at about 6.4
instructions per byte, inside the budget.</p>

<p>Why not 100%, if the count is within budget? Being under the budget is necessary, not sufficient. At batch 8 and above
the paged kernel reaches 68–74% of peak bandwidth, against 93–95% for a kernel that only reads the cache, so the
remaining gap is in the decode work, not in the loads. Neither the bytes read nor the throughput of any one pipe is near
its limit there, and 57% of the kernel’s instructions reconstruct and scale 4-bit values, each through a short chain
of dependent integer and half-precision operations before the tensor core can use it. Instruction latency and these
dependency chains are the likely remaining limit; we could not confirm it, because GPU stall counters were not
available on our cluster, and we have not reduced it further. Moving K and V
onto FP8 tensor cores after a cheap bit placement (described in the report) raises it to 79–89%.</p>

<h2 id="what-you-get-in-vllm">What you get in vLLM</h2>

<p>Through a vLLM 0.33 plugin, with an accuracy recipe (K mean removal, K rotation, four BF16 sink tokens, layer 0 in
BF16), against vLLM’s FP8 KV cache on FlashInfer with XQA decode: (none of the recipe’s parts is new: K mean removal follows
SageAttention’s “smooth K”, the Hadamard rotation QuaRot, the BF16 sink tokens StreamingLLM and IntactKV, and keeping
a sensitive layer in higher precision is common practice; the kernel is the new part)</p>

<table>
  <thead>
    <tr>
      <th> </th>
      <th>Ours</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tokens the KV cache holds</td>
      <td>1.63× as many</td>
    </tr>
    <tr>
      <td>Decode step, Llama-3.1-8B, batch 8 and above</td>
      <td>1.12–1.64× faster</td>
    </tr>
    <tr>
      <td>Decode step, batch 1</td>
      <td>level</td>
    </tr>
    <tr>
      <td>Decode step, Qwen3-32B and Qwen2.5-72B</td>
      <td>1.04–1.18× faster (the weight read dominates the step)</td>
    </tr>
    <tr>
      <td>Time to first token</td>
      <td>1.5–6% longer</td>
    </tr>
    <tr>
      <td>Accuracy against BF16 KV (five models, repeated runs)</td>
      <td>the 95% intervals exclude losses larger than about 3.5 points</td>
    </tr>
    <tr>
      <td>Accuracy at 120K tokens (Llama-3.1-8B, multi-key retrieval)</td>
      <td>15 points below BF16 KV (vLLM’s default FP8 path: 13 below)</td>
    </tr>
    <tr>
      <td>Decode kernel alone, 11 shapes</td>
      <td>1.10–1.48× faster than the faster of XQA and FlashAttention-4’s native FP8</td>
    </tr>
  </tbody>
</table>

<p><strong>Serving under load: two different numbers.</strong> The larger cache helps most when traffic spikes. Starting from an
empty cache, one H200 absorbed a 600-second burst of a reasoning-like stream (1K input, 8K output tokens) at
1.23–1.32× FP8’s request rate on Llama-3.1-8B, 1.45–1.80× on Qwen3-32B and 1.67–2.20× on Qwen2.5-72B; at the higher
rates FP8’s cache filled and requests were preempted while ours stayed at or below 81% cache use on Llama-3.1-8B. These rest on one
seed at most points. Whether a GPU also <em>sustains</em> a higher rate is a different question, and we did not settle it.
A steady-state test with rules fixed in advance showed no gain at its planned rates on any of the three models; the
planned rates, taken from the burst results, turned out to be too high for our configuration. Rates we added after
those results put the sustained gain at no less than about 1.31× on Qwen3-32B and 1.14× on Qwen2.5-72B, and include no
gain on Llama-3.1-8B; because they were chosen after seeing data and mostly have one seed, the report does not claim a
sustained gain. If your traffic is bursty, the first number is the relevant one; for steady traffic, measure on your
own workload.</p>

<p>Where it does not help: batch-1 latency, and streams with long inputs and short outputs, where prefill rather than the
cache is the limit. And very long contexts cost accuracy on models not trained for them: at 120K tokens on
Llama-3.1-8B our cache loses 15 points against BF16 KV. Offline, FP8 storage alone loses only 3 points on the same
trials and our cache 10, so at that length the 4-bit cache costs accuracy that FP8 storage does not; on
Llama-3-8B-ProLong-512k, trained for long context, it stays within 1.5 points of BF16.</p>

<h2 id="try-it">Try it</h2>

<p>In a vLLM 0.33.0 environment on H100 or H200:</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/thomaszhang2661/kv4-hopper <span class="o">&amp;&amp;</span> <span class="nb">cd </span>kv4-hopper
pip <span class="nb">install</span> <span class="nt">--no-deps</span> <span class="nt">-e</span> integrations/vllm_plugin
<span class="nb">export </span><span class="nv">PYTHONPATH</span><span class="o">=</span><span class="nv">$PWD</span>:<span class="nv">$PWD</span>/integrations/vllm_plugin <span class="nv">TORCH_CUDA_ARCH_LIST</span><span class="o">=</span>9.0a
<span class="nb">export </span><span class="nv">KV4_VLLM</span><span class="o">=</span>1 <span class="nv">KV4_CALIB</span><span class="o">=</span>results/calib/Llama-3.1-8B.pt <span class="nv">KV4_FORMAT</span><span class="o">=</span>nvfp4 <span class="nv">KV4_KMEAN</span><span class="o">=</span>1 <span class="nv">KV4_SINKS</span><span class="o">=</span>1 <span class="nv">KV4_BOUNDARY</span><span class="o">=</span>0
vllm serve meta-llama/Llama-3.1-8B <span class="nt">--kv-cache-dtype</span> ultraquant_4bit <span class="nt">--block-size</span> 64
</code></pre></div></div>

<p>Calibration files for the ten models in the report are in <code class="language-plaintext highlighter-rouge">results/calib/</code>; <code class="language-plaintext highlighter-rouge">bench/calib_kv.py</code> makes one for another
model. To compare against FP8 on your own GPU, <code class="language-plaintext highlighter-rouge">bench/vllm_forward_matrix.py</code> times the pure decode step of both
configurations in one process.</p>

<p>Code: <a href="https://github.com/thomaszhang2661/kv4-hopper">github.com/thomaszhang2661/kv4-hopper</a> · Report: <a href="https://arxiv.org/abs/2611.30027v2">arXiv:2611.30027v2</a></p>

<p>Upstream: the decode kernel is proposed to FlashInfer in <a href="https://github.com/flashinfer-ai/flashinfer/pull/10152">#10152</a>, and the vLLM wiring in RFC <a href="https://github.com/vllm-project/vllm/issues/70766">#70766</a>.</p>

<h2 id="limits">Limits</h2>

<p>Long contexts on models not trained for them lose accuracy (above). Head dimension 128 and up to 8 query heads per KV
head; no MLA models, sliding-window layers or speculative decoding;
the vLLM plugin is tied to vLLM 0.33. Kernel and vLLM numbers are from H200; the kernels also run on H100. On
Blackwell, which converts FP4 in hardware, we expect the decode gap between software unpacking and native FP4 to be
small at small batch and the native path to win at large batch and in prefill, but we have not measured it.</p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="kv-cache" /><category term="quantization" /><category term="vllm" /><category term="cuda" /><summary type="html"><![CDATA[On H200, vLLM's 4-bit KV caches decode slower than FP8 because Hopper has no FP4 hardware and software unpacking blows the instruction budget (about 7 instructions per byte). A kernel built around that budget, and what it gives inside vLLM.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://thomaszhang2661.github.io/assets/hopper_issue_budget.png" /><media:content medium="image" url="https://thomaszhang2661.github.io/assets/hopper_issue_budget.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="zh-CN"><title type="html">Adam 和 Newton 都在调步长，差在哪：一个斜放的山谷</title><link href="https://thomaszhang2661.github.io/2026/11/26/adam-vs-newton-zh/" rel="alternate" type="text/html" title="Adam 和 Newton 都在调步长，差在哪：一个斜放的山谷" /><published>2026-11-26T00:00:00+00:00</published><updated>2026-11-26T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/26/adam-vs-newton-zh</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/26/adam-vs-newton-zh/"><![CDATA[<p><em>标签：优化。这篇接着<a href="/2026/11/23/newton-gutzwiller-self-consistency-zh/">上一篇讲 Newton 法和自洽迭代的文章</a>。</em></p>

<p><a href="/2026/11/26/adam-vs-newton/">English version</a></p>

<p>写完上一篇，一个很自然的问题是：Adam 不也是按方向调步长吗，它和 Newton 法的思路不是一样的？</p>

<p>一半对。它们想做的事确实一样，用的零件不一样。这篇先用一张图把直觉讲清楚，再写数学。文中的数字都来自同一个两变量的小例子，是实际跑出来的，不是估算。</p>

<h2 id="一个又长又窄的山谷">一个又长又窄的山谷</h2>

<p>想象你蒙着眼睛下山，要走到谷底。你只能感觉到脚下往哪边斜、斜得多厉害。</p>

<p>最简单的走法是梯度下降：哪边往下斜就往哪边迈一步，每步长度固定。麻烦在于山谷往往又长又窄：横着很陡，顺着很平。步子迈大了，你在两侧的陡坡之间来回撞；迈小了，顺着平缓的谷底要走很久。</p>

<p>下图左边就是这样一个山谷：顺着谷底的方向曲率是 0.05，横过谷底的方向是 1.5，差 30 倍。蓝线是梯度下降，步长已经取到最好，还是要来回撞 208 步，误差才降到初始的百万分之一。</p>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 740 460" width="100%" role="img" aria-label="同一个山谷，两种摆法" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Noto Sans SC', Helvetica, Arial, sans-serif;max-width:740px">
<rect width="740" height="460" fill="#fcfcfb" />
<text x="20" y="32" font-size="19" font-weight="600" fill="#0b0b0b">同一个山谷，两种摆法</text>
<text x="20" y="54" font-size="12.5" fill="#52514e">L = ½ xᵀHx 的等高线，两个曲率为 0.05 和 1.5（κ = 30）；初值相同；各方法的前若干步</text>
<clipPath id="clip0"><rect x="40" y="120" width="320" height="250" /></clipPath>
<rect x="40" y="120" width="320" height="250" fill="#ffffff" stroke="#d6d5d0" />
<text x="40" y="108" font-size="13.5" font-weight="600" fill="#0b0b0b">(a) 沿坐标轴摆放</text>
<g clip-path="url(#clip0)">
<path d="M236.2,245.0 L236.2,244.7 L236.0,244.3 L235.8,244.0 L235.4,243.6 L235.0,243.3 L234.4,243.0 L233.8,242.6 L233.1,242.3 L232.3,242.0 L231.4,241.7 L230.4,241.4 L229.3,241.1 L228.1,240.8 L226.9,240.6 L225.6,240.3 L224.2,240.1 L222.8,239.9 L221.3,239.7 L219.7,239.5 L218.1,239.3 L216.4,239.1 L214.7,239.0 L213.0,238.8 L211.2,238.7 L209.4,238.6 L207.5,238.5 L205.7,238.5 L203.8,238.4 L201.9,238.4 L200.0,238.4 L198.1,238.4 L196.2,238.4 L194.3,238.5 L192.5,238.5 L190.6,238.6 L188.8,238.7 L187.0,238.8 L185.3,239.0 L183.6,239.1 L181.9,239.3 L180.3,239.5 L178.7,239.7 L177.2,239.9 L175.8,240.1 L174.4,240.3 L173.1,240.6 L171.9,240.8 L170.7,241.1 L169.6,241.4 L168.6,241.7 L167.7,242.0 L166.9,242.3 L166.2,242.6 L165.6,243.0 L165.0,243.3 L164.6,243.6 L164.2,244.0 L164.0,244.3 L163.8,244.7 L163.8,245.0 L163.8,245.3 L164.0,245.7 L164.2,246.0 L164.6,246.4 L165.0,246.7 L165.6,247.0 L166.2,247.4 L166.9,247.7 L167.7,248.0 L168.6,248.3 L169.6,248.6 L170.7,248.9 L171.9,249.2 L173.1,249.4 L174.4,249.7 L175.8,249.9 L177.2,250.1 L178.7,250.3 L180.3,250.5 L181.9,250.7 L183.6,250.9 L185.3,251.0 L187.0,251.2 L188.8,251.3 L190.6,251.4 L192.5,251.5 L194.3,251.5 L196.2,251.6 L198.1,251.6 L200.0,251.6 L201.9,251.6 L203.8,251.6 L205.7,251.5 L207.5,251.5 L209.4,251.4 L211.2,251.3 L213.0,251.2 L214.7,251.0 L216.4,250.9 L218.1,250.7 L219.7,250.5 L221.3,250.3 L222.8,250.1 L224.2,249.9 L225.6,249.7 L226.9,249.4 L228.1,249.2 L229.3,248.9 L230.4,248.6 L231.4,248.3 L232.3,248.0 L233.1,247.7 L233.8,247.4 L234.4,247.0 L235.0,246.7 L235.4,246.4 L235.8,246.0 L236.0,245.7 L236.2,245.3 L236.2,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M272.4,245.0 L272.3,244.3 L272.0,243.6 L271.5,242.9 L270.8,242.3 L269.9,241.6 L268.9,240.9 L267.6,240.3 L266.1,239.6 L264.5,239.0 L262.7,238.4 L260.7,237.8 L258.6,237.2 L256.3,236.7 L253.8,236.2 L251.2,235.7 L248.5,235.2 L245.6,234.7 L242.6,234.3 L239.4,233.9 L236.2,233.6 L232.9,233.2 L229.5,232.9 L225.9,232.7 L222.4,232.4 L218.7,232.2 L215.1,232.1 L211.3,231.9 L207.6,231.9 L203.8,231.8 L200.0,231.8 L196.2,231.8 L192.4,231.9 L188.7,231.9 L184.9,232.1 L181.3,232.2 L177.6,232.4 L174.1,232.7 L170.5,232.9 L167.1,233.2 L163.8,233.6 L160.6,233.9 L157.4,234.3 L154.4,234.7 L151.5,235.2 L148.8,235.7 L146.2,236.2 L143.7,236.7 L141.4,237.2 L139.3,237.8 L137.3,238.4 L135.5,239.0 L133.9,239.6 L132.4,240.3 L131.1,240.9 L130.1,241.6 L129.2,242.3 L128.5,242.9 L128.0,243.6 L127.7,244.3 L127.6,245.0 L127.7,245.7 L128.0,246.4 L128.5,247.1 L129.2,247.7 L130.1,248.4 L131.1,249.1 L132.4,249.7 L133.9,250.4 L135.5,251.0 L137.3,251.6 L139.3,252.2 L141.4,252.8 L143.7,253.3 L146.2,253.8 L148.8,254.3 L151.5,254.8 L154.4,255.3 L157.4,255.7 L160.6,256.1 L163.8,256.4 L167.1,256.8 L170.5,257.1 L174.1,257.3 L177.6,257.6 L181.3,257.8 L184.9,257.9 L188.7,258.1 L192.4,258.1 L196.2,258.2 L200.0,258.2 L203.8,258.2 L207.6,258.1 L211.3,258.1 L215.1,257.9 L218.7,257.8 L222.4,257.6 L225.9,257.3 L229.5,257.1 L232.9,256.8 L236.2,256.4 L239.4,256.1 L242.6,255.7 L245.6,255.3 L248.5,254.8 L251.2,254.3 L253.8,253.8 L256.3,253.3 L258.6,252.8 L260.7,252.2 L262.7,251.6 L264.5,251.0 L266.1,250.4 L267.6,249.7 L268.9,249.1 L269.9,248.4 L270.8,247.7 L271.5,247.1 L272.0,246.4 L272.3,245.7 L272.4,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M314.5,245.0 L314.3,243.9 L313.9,242.8 L313.1,241.7 L312.0,240.7 L310.6,239.6 L308.9,238.5 L306.9,237.5 L304.6,236.5 L302.0,235.5 L299.1,234.5 L296.0,233.6 L292.6,232.7 L289.0,231.8 L285.1,231.0 L281.0,230.2 L276.6,229.5 L272.0,228.8 L267.3,228.1 L262.4,227.5 L257.2,226.9 L252.0,226.4 L246.6,225.9 L241.0,225.5 L235.4,225.1 L229.6,224.8 L223.8,224.6 L217.9,224.4 L212.0,224.2 L206.0,224.1 L200.0,224.1 L194.0,224.1 L188.0,224.2 L182.1,224.4 L176.2,224.6 L170.4,224.8 L164.6,225.1 L159.0,225.5 L153.4,225.9 L148.0,226.4 L142.8,226.9 L137.6,227.5 L132.7,228.1 L128.0,228.8 L123.4,229.5 L119.0,230.2 L114.9,231.0 L111.0,231.8 L107.4,232.7 L104.0,233.6 L100.9,234.5 L98.0,235.5 L95.4,236.5 L93.1,237.5 L91.1,238.5 L89.4,239.6 L88.0,240.7 L86.9,241.7 L86.1,242.8 L85.7,243.9 L85.5,245.0 L85.7,246.1 L86.1,247.2 L86.9,248.3 L88.0,249.3 L89.4,250.4 L91.1,251.5 L93.1,252.5 L95.4,253.5 L98.0,254.5 L100.9,255.5 L104.0,256.4 L107.4,257.3 L111.0,258.2 L114.9,259.0 L119.0,259.8 L123.4,260.5 L128.0,261.2 L132.7,261.9 L137.6,262.5 L142.8,263.1 L148.0,263.6 L153.4,264.1 L159.0,264.5 L164.6,264.9 L170.4,265.2 L176.2,265.4 L182.1,265.6 L188.0,265.8 L194.0,265.9 L200.0,265.9 L206.0,265.9 L212.0,265.8 L217.9,265.6 L223.8,265.4 L229.6,265.2 L235.4,264.9 L241.0,264.5 L246.6,264.1 L252.0,263.6 L257.2,263.1 L262.4,262.5 L267.3,261.9 L272.0,261.2 L276.6,260.5 L281.0,259.8 L285.1,259.0 L289.0,258.2 L292.6,257.3 L296.0,256.4 L299.1,255.5 L302.0,254.5 L304.6,253.5 L306.9,252.5 L308.9,251.5 L310.6,250.4 L312.0,249.3 L313.1,248.3 L313.9,247.2 L314.3,246.1 L314.5,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M381.0,245.0 L380.8,243.3 L380.0,241.5 L378.8,239.8 L377.1,238.1 L374.9,236.4 L372.2,234.8 L369.0,233.2 L365.4,231.6 L361.3,230.0 L356.8,228.5 L351.8,227.0 L346.4,225.6 L340.7,224.2 L334.5,222.9 L328.0,221.6 L321.1,220.4 L313.9,219.3 L306.4,218.3 L298.6,217.3 L290.5,216.4 L282.2,215.6 L273.6,214.8 L264.9,214.1 L255.9,213.6 L246.9,213.1 L237.6,212.7 L228.3,212.4 L218.9,212.1 L209.5,212.0 L200.0,212.0 L190.5,212.0 L181.1,212.1 L171.7,212.4 L162.4,212.7 L153.1,213.1 L144.1,213.6 L135.1,214.1 L126.4,214.8 L117.8,215.6 L109.5,216.4 L101.4,217.3 L93.6,218.3 L86.1,219.3 L78.9,220.4 L72.0,221.6 L65.5,222.9 L59.3,224.2 L53.6,225.6 L48.2,227.0 L43.2,228.5 L38.7,230.0 L34.6,231.6 L31.0,233.2 L27.8,234.8 L25.1,236.4 L22.9,238.1 L21.2,239.8 L20.0,241.5 L19.2,243.3 L19.0,245.0 L19.2,246.7 L20.0,248.5 L21.2,250.2 L22.9,251.9 L25.1,253.6 L27.8,255.2 L31.0,256.8 L34.6,258.4 L38.7,260.0 L43.2,261.5 L48.2,263.0 L53.6,264.4 L59.3,265.8 L65.5,267.1 L72.0,268.4 L78.9,269.6 L86.1,270.7 L93.6,271.7 L101.4,272.7 L109.5,273.6 L117.8,274.4 L126.4,275.2 L135.1,275.9 L144.1,276.4 L153.1,276.9 L162.4,277.3 L171.7,277.6 L181.1,277.9 L190.5,278.0 L200.0,278.0 L209.5,278.0 L218.9,277.9 L228.3,277.6 L237.6,277.3 L246.9,276.9 L255.9,276.4 L264.9,275.9 L273.6,275.2 L282.2,274.4 L290.5,273.6 L298.6,272.7 L306.4,271.7 L313.9,270.7 L321.1,269.6 L328.0,268.4 L334.5,267.1 L340.7,265.8 L346.4,264.4 L351.8,263.0 L356.8,261.5 L361.3,260.0 L365.4,258.4 L369.0,256.8 L372.2,255.2 L374.9,253.6 L377.1,251.9 L378.8,250.2 L380.0,248.5 L380.8,246.7 L381.0,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M456.0,245.0 L455.6,242.6 L454.6,240.1 L452.8,237.7 L450.4,235.3 L447.3,232.9 L443.5,230.6 L439.0,228.3 L433.9,226.0 L428.1,223.8 L421.7,221.6 L414.7,219.5 L407.1,217.5 L398.9,215.6 L390.2,213.7 L381.0,212.0 L371.3,210.3 L361.1,208.7 L350.5,207.2 L339.4,205.8 L328.0,204.5 L316.2,203.4 L304.1,202.3 L291.7,201.4 L279.1,200.5 L266.3,199.9 L253.2,199.3 L240.0,198.8 L226.8,198.5 L213.4,198.3 L200.0,198.3 L186.6,198.3 L173.2,198.5 L160.0,198.8 L146.8,199.3 L133.7,199.9 L120.9,200.5 L108.3,201.4 L95.9,202.3 L83.8,203.4 L72.0,204.5 L60.6,205.8 L49.5,207.2 L38.9,208.7 L28.7,210.3 L19.0,212.0 L9.8,213.7 L1.1,215.6 L-7.1,217.5 L-14.7,219.5 L-21.7,221.6 L-28.1,223.8 L-33.9,226.0 L-39.0,228.3 L-43.5,230.6 L-47.3,232.9 L-50.4,235.3 L-52.8,237.7 L-54.6,240.1 L-55.6,242.6 L-56.0,245.0 L-55.6,247.4 L-54.6,249.9 L-52.8,252.3 L-50.4,254.7 L-47.3,257.1 L-43.5,259.4 L-39.0,261.7 L-33.9,264.0 L-28.1,266.2 L-21.7,268.4 L-14.7,270.5 L-7.1,272.5 L1.1,274.4 L9.8,276.3 L19.0,278.0 L28.7,279.7 L38.9,281.3 L49.5,282.8 L60.6,284.2 L72.0,285.5 L83.8,286.6 L95.9,287.7 L108.3,288.6 L120.9,289.5 L133.7,290.1 L146.8,290.7 L160.0,291.2 L173.2,291.5 L186.6,291.7 L200.0,291.7 L213.4,291.7 L226.8,291.5 L240.0,291.2 L253.2,290.7 L266.3,290.1 L279.1,289.5 L291.7,288.6 L304.1,287.7 L316.2,286.6 L328.0,285.5 L339.4,284.2 L350.5,282.8 L361.1,281.3 L371.3,279.7 L381.0,278.0 L390.2,276.3 L398.9,274.4 L407.1,272.5 L414.7,270.5 L421.7,268.4 L428.1,266.2 L433.9,264.0 L439.0,261.7 L443.5,259.4 L447.3,257.1 L450.4,254.7 L452.8,252.3 L454.6,249.9 L455.6,247.4 L456.0,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M562.0,245.0 L561.5,241.5 L560.1,238.1 L557.6,234.7 L554.1,231.3 L549.7,227.9 L544.3,224.6 L538.0,221.3 L530.7,218.1 L522.6,215.0 L513.5,212.0 L503.6,209.0 L492.9,206.1 L481.4,203.4 L469.0,200.8 L456.0,198.3 L442.3,195.9 L427.8,193.6 L412.8,191.5 L397.2,189.6 L381.0,187.8 L364.4,186.1 L347.3,184.6 L329.7,183.3 L311.9,182.1 L293.7,181.2 L275.3,180.3 L256.6,179.7 L237.8,179.3 L218.9,179.0 L200.0,178.9 L181.1,179.0 L162.2,179.3 L143.4,179.7 L124.7,180.3 L106.3,181.2 L88.1,182.1 L70.3,183.3 L52.7,184.6 L35.6,186.1 L19.0,187.8 L2.8,189.6 L-12.8,191.5 L-27.8,193.6 L-42.3,195.9 L-56.0,198.3 L-69.0,200.8 L-81.4,203.4 L-92.9,206.1 L-103.6,209.0 L-113.5,212.0 L-122.6,215.0 L-130.7,218.1 L-138.0,221.3 L-144.3,224.6 L-149.7,227.9 L-154.1,231.3 L-157.6,234.7 L-160.1,238.1 L-161.5,241.5 L-162.0,245.0 L-161.5,248.5 L-160.1,251.9 L-157.6,255.3 L-154.1,258.7 L-149.7,262.1 L-144.3,265.4 L-138.0,268.7 L-130.7,271.9 L-122.6,275.0 L-113.5,278.0 L-103.6,281.0 L-92.9,283.9 L-81.4,286.6 L-69.0,289.2 L-56.0,291.7 L-42.3,294.1 L-27.8,296.4 L-12.8,298.5 L2.8,300.4 L19.0,302.2 L35.6,303.9 L52.7,305.4 L70.3,306.7 L88.1,307.9 L106.3,308.8 L124.7,309.7 L143.4,310.3 L162.2,310.7 L181.1,311.0 L200.0,311.1 L218.9,311.0 L237.8,310.7 L256.6,310.3 L275.3,309.7 L293.7,308.8 L311.9,307.9 L329.7,306.7 L347.3,305.4 L364.4,303.9 L381.0,302.2 L397.2,300.4 L412.8,298.5 L427.8,296.4 L442.3,294.1 L456.0,291.7 L469.0,289.2 L481.4,286.6 L492.9,283.9 L503.6,281.0 L513.5,278.0 L522.6,275.0 L530.7,271.9 L538.0,268.7 L544.3,265.4 L549.7,262.1 L554.1,258.7 L557.6,255.3 L560.1,251.9 L561.5,248.5 L562.0,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M328.0,181.0 L319.7,304.9 L312.0,189.0 L304.8,297.4 L298.0,196.0 L291.7,290.9 L285.8,202.1 L280.3,285.1 L275.1,207.5 L270.2,280.1 L265.7,212.1 L261.5,275.7 L257.5,216.3 L253.8,271.9 L250.3,219.8 L247.1,268.5 L244.0,223.0 L241.2,265.6 L238.5,225.7 L236.0,263.0 L233.7,228.1 L231.5,260.8 L229.5,230.2 L227.6,258.8 L225.8,232.1 L224.2,257.1 L222.6,233.7 L221.1,255.6 L219.8,235.1 L218.5,254.3 L217.3,236.3 L216.2,253.1 L215.1,237.4 L214.2,252.1 L213.3,238.4 L212.4,251.2 L211.6,239.2 L210.9,250.4 L210.2,239.9 L209.5,249.7" fill="none" stroke="#2a78d6" stroke-width="1.6" stroke-linejoin="round" />
<path d="M328.0,181.0 L315.2,193.8 L302.5,206.4 L289.8,218.8 L277.3,230.5 L265.0,241.4 L253.0,251.1 L241.4,259.1 L230.2,265.1 L219.7,269.1 L209.8,271.0 L200.7,271.2 L192.5,269.8 L185.3,267.3 L179.1,263.8 L174.1,259.6 L170.2,255.1 L167.4,250.5 L165.7,245.9 L165.0,241.8 L165.3,238.2 L166.4,235.3 L168.2,233.3 L170.6,232.1 L173.6,231.7 L176.9,232.2 L180.5,233.4 L184.3,235.1 L188.1,237.3 L192.0,239.8 L195.7,242.3 L199.1,244.8 L202.3,247.1 L205.2,249.0 L207.6,250.5 L209.6,251.5 L211.2,252.0 L212.3,252.0 L212.9,251.6 L213.1,250.7 L212.9,249.5 L212.3,248.1 L211.4,246.7 L210.2,245.2 L208.9,243.9 L207.3,242.7 L205.7,241.9 L204.0,241.3 L202.4,241.1 L200.8,241.1 L199.4,241.5 L198.1,242.1 L197.0,242.9 L196.1,243.7 L195.4,244.6 L194.9,245.4 L194.6,246.1 L194.6,246.7 L194.8,247.1 L195.1,247.2 L195.6,247.2 L196.2,247.0 L196.8,246.6 L197.6,246.2 L198.3,245.7 L199.1,245.1 L199.8,244.6 L200.4,244.2 L201.0,243.9 L201.5,243.7 L201.9,243.7 L202.1,243.7 L202.3,243.9 L202.4,244.2 L202.3,244.5 L202.2,244.8 L202.0,245.1 L201.7,245.4 L201.4,245.6 L201.0,245.7 L200.7,245.8 L200.3,245.8 L200.0,245.7 L199.7,245.5 L199.4,245.3 L199.2,245.1 L199.1,244.9 L199.0,244.8 L198.9,244.6 L198.9,244.6 L199.0,244.5 L199.1,244.6 L199.2,244.6 L199.3,244.7 L199.5,244.8 L199.7,245.0 L199.8,245.1 L200.0,245.2 L200.2,245.2 L200.3,245.3 L200.4,245.3 L200.4,245.3 L200.5,245.2 L200.5,245.1 L200.5,245.1 L200.5,245.0 L200.4,244.9 L200.3,244.9 L200.3,244.8 L200.2,244.8 L200.1,244.8 L200.0,244.9 L199.9,244.9 L199.9,245.0 L199.8,245.0 L199.8,245.0 L199.8,245.1 L199.8,245.1 L199.8,245.1 L199.8,245.1" fill="none" stroke="#eb6834" stroke-width="1.6" stroke-linejoin="round" />
<path d="M328.0,181.0 L200.0,245.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round" />
<circle cx="328.0" cy="181.0" r="3.2" fill="#0b0b0b" />
<circle cx="200.0" cy="245.0" r="3.2" fill="#0b0b0b" />
<path d="M328.0,181.0 L200.0,245.0" fill="none" stroke="#1baf7a" stroke-width="1.6" stroke-linejoin="round" stroke-dasharray="5 4" />
</g>
<circle cx="328.0" cy="181.0" r="4" fill="none" stroke="#0b0b0b" stroke-width="1.5" />
<text x="200.0" y="249.0" font-size="14" fill="#0b0b0b" text-anchor="middle">×</text>
<rect x="40" y="386" width="14" height="3" fill="#2a78d6" />
<text x="60" y="392" font-size="12" fill="#0b0b0b">梯度下降：208 步</text>
<rect x="40" y="403" width="14" height="3" fill="#eb6834" />
<text x="60" y="409" font-size="12" fill="#0b0b0b">Adam：213 步</text>
<rect x="40" y="420" width="14" height="3" fill="#0b0b0b" />
<text x="60" y="426" font-size="12" fill="#0b0b0b">Newton：1 步</text>
<rect x="40" y="437" width="14" height="3" fill="#1baf7a" />
<text x="60" y="443" font-size="12" fill="#0b0b0b">按曲率的对角缩放：1 步（与 Newton 重合）</text>
<clipPath id="clip1"><rect x="380" y="120" width="320" height="250" /></clipPath>
<rect x="380" y="120" width="320" height="250" fill="#ffffff" stroke="#d6d5d0" />
<text x="380" y="108" font-size="13.5" font-weight="600" fill="#0b0b0b">(b) 转 45° 斜放</text>
<g clip-path="url(#clip1)">
<path d="M565.6,219.4 L565.3,219.2 L565.0,219.1 L564.6,219.0 L564.1,219.0 L563.5,219.1 L562.9,219.2 L562.2,219.4 L561.5,219.7 L560.7,220.1 L559.8,220.5 L558.9,221.0 L558.0,221.5 L557.0,222.2 L555.9,222.8 L554.8,223.6 L553.7,224.4 L552.5,225.3 L551.3,226.2 L550.0,227.1 L548.8,228.2 L547.5,229.2 L546.1,230.3 L544.8,231.5 L543.5,232.6 L542.1,233.9 L540.8,235.1 L539.4,236.4 L538.0,237.7 L536.7,239.0 L535.3,240.3 L534.0,241.7 L532.7,243.0 L531.4,244.4 L530.1,245.8 L528.9,247.1 L527.6,248.5 L526.5,249.8 L525.3,251.1 L524.2,252.5 L523.2,253.8 L522.1,255.0 L521.2,256.3 L520.3,257.5 L519.4,258.7 L518.6,259.8 L517.8,260.9 L517.2,262.0 L516.5,263.0 L516.0,263.9 L515.5,264.8 L515.1,265.7 L514.7,266.5 L514.4,267.2 L514.2,267.9 L514.1,268.5 L514.0,269.1 L514.0,269.6 L514.1,270.0 L514.2,270.3 L514.4,270.6 L514.7,270.8 L515.0,270.9 L515.4,271.0 L515.9,271.0 L516.5,270.9 L517.1,270.8 L517.8,270.6 L518.5,270.3 L519.3,269.9 L520.2,269.5 L521.1,269.0 L522.0,268.5 L523.0,267.8 L524.1,267.2 L525.2,266.4 L526.3,265.6 L527.5,264.7 L528.7,263.8 L530.0,262.9 L531.2,261.8 L532.5,260.8 L533.9,259.7 L535.2,258.5 L536.5,257.4 L537.9,256.1 L539.2,254.9 L540.6,253.6 L542.0,252.3 L543.3,251.0 L544.7,249.7 L546.0,248.3 L547.3,247.0 L548.6,245.6 L549.9,244.2 L551.1,242.9 L552.4,241.5 L553.5,240.2 L554.7,238.9 L555.8,237.5 L556.8,236.2 L557.9,235.0 L558.8,233.7 L559.7,232.5 L560.6,231.3 L561.4,230.2 L562.2,229.1 L562.8,228.0 L563.5,227.0 L564.0,226.1 L564.5,225.2 L564.9,224.3 L565.3,223.5 L565.6,222.8 L565.8,222.1 L565.9,221.5 L566.0,220.9 L566.0,220.4 L565.9,220.0 L565.8,219.7 L565.6,219.4" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M591.2,193.8 L590.6,193.4 L589.9,193.1 L589.1,193.0 L588.1,193.0 L587.0,193.1 L585.8,193.4 L584.4,193.9 L583.0,194.4 L581.4,195.1 L579.7,196.0 L577.8,197.0 L575.9,198.1 L573.9,199.3 L571.8,200.7 L569.6,202.2 L567.3,203.8 L565.0,205.5 L562.5,207.3 L560.0,209.3 L557.5,211.3 L554.9,213.4 L552.3,215.6 L549.6,217.9 L546.9,220.3 L544.2,222.7 L541.5,225.2 L538.8,227.8 L536.1,230.4 L533.3,233.0 L530.7,235.7 L528.0,238.3 L525.4,241.1 L522.8,243.8 L520.2,246.5 L517.7,249.2 L515.3,251.9 L512.9,254.6 L510.6,257.3 L508.4,259.9 L506.3,262.5 L504.3,265.0 L502.3,267.5 L500.5,270.0 L498.8,272.3 L497.2,274.6 L495.7,276.8 L494.3,278.9 L493.1,280.9 L492.0,282.8 L491.0,284.7 L490.1,286.4 L489.4,288.0 L488.9,289.4 L488.4,290.8 L488.1,292.0 L488.0,293.1 L488.0,294.1 L488.1,294.9 L488.4,295.6 L488.8,296.2 L489.4,296.6 L490.1,296.9 L490.9,297.0 L491.9,297.0 L493.0,296.9 L494.2,296.6 L495.6,296.1 L497.0,295.6 L498.6,294.9 L500.3,294.0 L502.2,293.0 L504.1,291.9 L506.1,290.7 L508.2,289.3 L510.4,287.8 L512.7,286.2 L515.0,284.5 L517.5,282.7 L520.0,280.7 L522.5,278.7 L525.1,276.6 L527.7,274.4 L530.4,272.1 L533.1,269.7 L535.8,267.3 L538.5,264.8 L541.2,262.2 L543.9,259.6 L546.7,257.0 L549.3,254.3 L552.0,251.7 L554.6,248.9 L557.2,246.2 L559.8,243.5 L562.3,240.8 L564.7,238.1 L567.1,235.4 L569.4,232.7 L571.6,230.1 L573.7,227.5 L575.7,225.0 L577.7,222.5 L579.5,220.0 L581.2,217.7 L582.8,215.4 L584.3,213.2 L585.7,211.1 L586.9,209.1 L588.0,207.2 L589.0,205.3 L589.9,203.6 L590.6,202.0 L591.1,200.6 L591.6,199.2 L591.9,198.0 L592.0,196.9 L592.0,195.9 L591.9,195.1 L591.6,194.4 L591.2,193.8" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M621.0,164.0 L620.1,163.4 L619.0,162.9 L617.6,162.7 L616.1,162.7 L614.4,163.0 L612.4,163.4 L610.3,164.1 L607.9,165.0 L605.4,166.2 L602.7,167.5 L599.8,169.1 L596.8,170.8 L593.6,172.8 L590.3,174.9 L586.8,177.3 L583.2,179.8 L579.5,182.6 L575.6,185.5 L571.7,188.5 L567.7,191.7 L563.6,195.1 L559.4,198.6 L555.2,202.2 L551.0,205.9 L546.7,209.8 L542.4,213.7 L538.1,217.7 L533.8,221.8 L529.5,226.0 L525.2,230.2 L521.0,234.5 L516.8,238.8 L512.7,243.1 L508.7,247.4 L504.8,251.7 L500.9,256.0 L497.2,260.2 L493.6,264.4 L490.1,268.6 L486.7,272.7 L483.5,276.7 L480.5,280.6 L477.6,284.5 L474.8,288.2 L472.3,291.8 L469.9,295.3 L467.8,298.6 L465.8,301.8 L464.1,304.8 L462.5,307.7 L461.2,310.4 L460.0,312.9 L459.1,315.3 L458.4,317.4 L458.0,319.4 L457.7,321.1 L457.7,322.6 L457.9,324.0 L458.4,325.1 L459.0,326.0 L459.9,326.6 L461.0,327.1 L462.4,327.3 L463.9,327.3 L465.6,327.0 L467.6,326.6 L469.7,325.9 L472.1,325.0 L474.6,323.8 L477.3,322.5 L480.2,320.9 L483.2,319.2 L486.4,317.2 L489.7,315.1 L493.2,312.7 L496.8,310.2 L500.5,307.4 L504.4,304.5 L508.3,301.5 L512.3,298.3 L516.4,294.9 L520.6,291.4 L524.8,287.8 L529.0,284.1 L533.3,280.2 L537.6,276.3 L541.9,272.3 L546.2,268.2 L550.5,264.0 L554.8,259.8 L559.0,255.5 L563.2,251.2 L567.3,246.9 L571.3,242.6 L575.2,238.3 L579.1,234.0 L582.8,229.8 L586.4,225.6 L589.9,221.4 L593.3,217.3 L596.5,213.3 L599.5,209.4 L602.4,205.5 L605.2,201.8 L607.7,198.2 L610.1,194.7 L612.2,191.4 L614.2,188.2 L615.9,185.2 L617.5,182.3 L618.8,179.6 L620.0,177.1 L620.9,174.7 L621.6,172.6 L622.0,170.6 L622.3,168.9 L622.3,167.4 L622.1,166.0 L621.6,164.9 L621.0,164.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M668.0,117.0 L666.6,116.0 L664.9,115.3 L662.8,114.9 L660.3,114.9 L657.6,115.3 L654.5,116.0 L651.1,117.1 L647.4,118.6 L643.4,120.3 L639.2,122.5 L634.6,124.9 L629.8,127.7 L624.8,130.8 L619.5,134.2 L614.0,138.0 L608.3,142.0 L602.4,146.3 L596.3,150.9 L590.1,155.7 L583.8,160.8 L577.3,166.1 L570.7,171.6 L564.1,177.3 L557.3,183.2 L550.6,189.3 L543.8,195.5 L536.9,201.9 L530.1,208.4 L523.4,215.0 L516.6,221.6 L510.0,228.4 L503.4,235.1 L496.9,241.9 L490.5,248.8 L484.3,255.6 L478.2,262.3 L472.3,269.1 L466.6,275.7 L461.1,282.3 L455.8,288.8 L450.7,295.1 L445.9,301.3 L441.3,307.4 L437.0,313.3 L433.0,319.0 L429.2,324.5 L425.8,329.8 L422.7,334.8 L419.9,339.6 L417.5,344.2 L415.3,348.4 L413.6,352.4 L412.1,356.1 L411.0,359.5 L410.3,362.6 L409.9,365.3 L409.9,367.8 L410.3,369.9 L411.0,371.6 L412.0,373.0 L413.4,374.0 L415.1,374.7 L417.2,375.1 L419.7,375.1 L422.4,374.7 L425.5,374.0 L428.9,372.9 L432.6,371.4 L436.6,369.7 L440.8,367.5 L445.4,365.1 L450.2,362.3 L455.2,359.2 L460.5,355.8 L466.0,352.0 L471.7,348.0 L477.6,343.7 L483.7,339.1 L489.9,334.3 L496.2,329.2 L502.7,323.9 L509.3,318.4 L515.9,312.7 L522.7,306.8 L529.4,300.7 L536.2,294.5 L543.1,288.1 L549.9,281.6 L556.6,275.0 L563.4,268.4 L570.0,261.6 L576.6,254.9 L583.1,248.1 L589.5,241.2 L595.7,234.4 L601.8,227.7 L607.7,220.9 L613.4,214.3 L618.9,207.7 L624.2,201.2 L629.3,194.9 L634.1,188.7 L638.7,182.6 L643.0,176.7 L647.0,171.0 L650.8,165.5 L654.2,160.2 L657.3,155.2 L660.1,150.4 L662.5,145.8 L664.7,141.6 L666.4,137.6 L667.9,133.9 L669.0,130.5 L669.7,127.4 L670.1,124.7 L670.1,122.2 L669.7,120.1 L669.0,118.4 L668.0,117.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M721.0,64.0 L719.0,62.5 L716.6,61.5 L713.6,61.0 L710.2,61.1 L706.3,61.6 L701.9,62.6 L697.2,64.2 L691.9,66.2 L686.3,68.7 L680.2,71.7 L673.8,75.2 L667.0,79.1 L659.9,83.5 L652.4,88.4 L644.6,93.6 L636.6,99.3 L628.2,105.4 L619.7,111.9 L610.9,118.7 L601.9,125.9 L592.7,133.4 L583.4,141.2 L574.0,149.3 L564.5,157.6 L554.9,166.2 L545.3,175.0 L535.7,184.0 L526.1,193.2 L516.5,202.5 L507.0,212.0 L497.5,221.5 L488.2,231.1 L479.0,240.7 L470.0,250.3 L461.2,259.9 L452.6,269.5 L444.3,279.0 L436.2,288.4 L428.4,297.7 L420.9,306.9 L413.7,315.9 L406.9,324.7 L400.4,333.2 L394.3,341.6 L388.6,349.6 L383.4,357.4 L378.5,364.9 L374.1,372.0 L370.2,378.8 L366.7,385.2 L363.7,391.3 L361.2,396.9 L359.2,402.2 L357.6,406.9 L356.6,411.3 L356.1,415.2 L356.0,418.6 L356.5,421.6 L357.5,424.0 L359.0,426.0 L361.0,427.5 L363.4,428.5 L366.4,429.0 L369.8,428.9 L373.7,428.4 L378.1,427.4 L382.8,425.8 L388.1,423.8 L393.7,421.3 L399.8,418.3 L406.2,414.8 L413.0,410.9 L420.1,406.5 L427.6,401.6 L435.4,396.4 L443.4,390.7 L451.8,384.6 L460.3,378.1 L469.1,371.3 L478.1,364.1 L487.3,356.6 L496.6,348.8 L506.0,340.7 L515.5,332.4 L525.1,323.8 L534.7,315.0 L544.3,306.0 L553.9,296.8 L563.5,287.5 L573.0,278.0 L582.5,268.5 L591.8,258.9 L601.0,249.3 L610.0,239.7 L618.8,230.1 L627.4,220.5 L635.7,211.0 L643.8,201.6 L651.6,192.3 L659.1,183.1 L666.3,174.1 L673.1,165.3 L679.6,156.8 L685.7,148.4 L691.4,140.4 L696.6,132.6 L701.5,125.1 L705.9,118.0 L709.8,111.2 L713.3,104.8 L716.3,98.7 L718.8,93.1 L720.8,87.8 L722.4,83.1 L723.4,78.7 L723.9,74.8 L724.0,71.4 L723.5,68.4 L722.5,66.0 L721.0,64.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M796.0,-11.0 L793.2,-13.1 L789.7,-14.5 L785.5,-15.2 L780.7,-15.1 L775.2,-14.4 L769.0,-12.9 L762.2,-10.7 L754.9,-7.9 L746.9,-4.3 L738.3,-0.1 L729.2,4.8 L719.6,10.4 L709.5,16.6 L699.0,23.5 L688.0,30.9 L676.6,39.0 L664.8,47.6 L652.7,56.7 L640.2,66.4 L627.5,76.5 L614.6,87.1 L601.4,98.2 L588.1,109.6 L574.7,121.4 L561.1,133.6 L547.5,146.1 L533.9,158.8 L520.3,171.8 L506.7,184.9 L493.3,198.3 L479.9,211.7 L466.8,225.3 L453.8,238.9 L441.1,252.5 L428.6,266.1 L416.4,279.7 L404.6,293.1 L393.2,306.4 L382.1,319.6 L371.5,332.5 L361.4,345.2 L351.7,357.7 L342.6,369.8 L334.0,381.6 L325.9,393.0 L318.5,404.0 L311.6,414.5 L305.4,424.6 L299.8,434.2 L294.9,443.3 L290.7,451.9 L287.1,459.9 L284.3,467.2 L282.1,474.0 L280.6,480.2 L279.9,485.7 L279.8,490.5 L280.5,494.7 L281.9,498.2 L284.0,501.0 L286.8,503.1 L290.3,504.5 L294.5,505.2 L299.3,505.1 L304.8,504.4 L311.0,502.9 L317.8,500.7 L325.1,497.9 L333.1,494.3 L341.7,490.1 L350.8,485.2 L360.4,479.6 L370.5,473.4 L381.0,466.5 L392.0,459.1 L403.4,451.0 L415.2,442.4 L427.3,433.3 L439.8,423.6 L452.5,413.5 L465.4,402.9 L478.6,391.8 L491.9,380.4 L505.3,368.6 L518.9,356.4 L532.5,343.9 L546.1,331.2 L559.7,318.2 L573.3,305.1 L586.7,291.7 L600.1,278.3 L613.2,264.7 L626.2,251.1 L638.9,237.5 L651.4,223.9 L663.6,210.3 L675.4,196.9 L686.8,183.6 L697.9,170.4 L708.5,157.5 L718.6,144.8 L728.3,132.3 L737.4,120.2 L746.0,108.4 L754.1,97.0 L761.5,86.0 L768.4,75.5 L774.6,65.4 L780.2,55.8 L785.1,46.7 L789.3,38.1 L792.9,30.1 L795.7,22.8 L797.9,16.0 L799.4,9.8 L800.1,4.3 L800.2,-0.5 L799.5,-4.7 L798.1,-8.2 L796.0,-11.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M668.0,181.0 L599.9,125.3 L652.0,189.0 L592.4,140.2 L638.0,196.0 L585.9,153.3 L625.8,202.1 L580.1,164.7 L615.1,207.5 L575.1,174.8 L605.7,212.1 L570.7,183.5 L597.5,216.3 L566.9,191.2 L590.3,219.8 L563.5,197.9 L584.0,223.0 L560.6,203.8 L578.5,225.7 L558.0,209.0 L573.7,228.1 L555.8,213.5 L569.5,230.2 L553.8,217.4 L565.8,232.1 L552.1,220.8 L562.6,233.7 L550.6,223.9 L559.8,235.1 L549.3,226.5 L557.3,236.3 L548.1,228.8 L555.1,237.4 L547.1,230.8 L553.3,238.4 L546.2,232.6 L551.6,239.2 L545.4,234.1 L550.2,239.9 L544.7,235.5" fill="none" stroke="#2a78d6" stroke-width="1.6" stroke-linejoin="round" />
<path d="M668.0,181.0 L627.5,140.5 L605.2,126.7 L605.0,136.8 L614.3,155.7 L624.4,175.8 L628.4,190.9 L623.9,197.8 L612.8,197.8 L598.2,193.9 L583.3,189.9 L571.5,188.9 L564.7,192.6 L562.7,200.8 L564.1,211.8 L566.5,223.5 L567.5,233.6 L565.7,240.3 L560.5,243.0 L552.7,242.7 L543.9,240.8 L535.9,239.1 L530.3,239.3 L527.8,241.9 L528.1,246.8 L530.2,252.9 L532.5,258.7 L533.7,262.7 L532.9,264.3 L530.2,263.4 L526.4,261.0 L522.7,258.1 L520.3,256.1 L519.8,255.7 L521.3,256.8 L524.2,258.9 L527.3,261.1 L529.7,262.1 L530.8,261.6 L530.4,259.6 L529.3,256.6 L528.3,253.5 L528.1,251.2 L529.2,250.2 L531.5,250.3 L534.3,251.0 L536.9,251.4 L538.6,251.1 L539.3,249.7 L539.0,247.6 L538.5,245.3 L538.3,243.4 L538.8,242.5 L540.1,242.5 L541.8,243.0 L543.4,243.6 L544.3,243.7 L544.4,243.1 L543.9,242.0 L543.1,240.9 L542.6,240.2 L542.6,240.1 L543.1,240.7 L543.8,241.5 L544.3,242.3 L544.3,242.7 L543.7,242.6 L542.9,242.2 L542.0,241.9 L541.5,241.9 L541.4,242.4 L541.5,243.2 L541.7,244.0 L541.7,244.6 L541.3,244.8 L540.6,244.7 L540.0,244.5 L539.5,244.5 L539.3,244.8 L539.4,245.3 L539.6,245.8 L539.7,246.1 L539.5,246.2 L539.2,246.1 L538.8,245.9 L538.6,245.7 L538.6,245.8 L538.9,245.9 L539.1,246.2 L539.3,246.2 L539.3,246.1 L539.2,245.9 L539.1,245.6 L539.1,245.4 L539.2,245.4 L539.5,245.4 L539.7,245.5 L539.9,245.5 L539.9,245.3 L539.9,245.1 L539.9,244.9 L539.9,244.8 L540.0,244.8 L540.2,244.9 L540.3,244.9 L540.4,244.9 L540.3,244.8 L540.3,244.6 L540.2,244.6 L540.2,244.6 L540.3,244.7 L540.4,244.7 L540.4,244.8 L540.3,244.8 L540.2,244.7 L540.2,244.7 L540.1,244.7 L540.1,244.8 L540.2,244.9 L540.2,245.0" fill="none" stroke="#eb6834" stroke-width="1.6" stroke-linejoin="round" />
<path d="M668.0,181.0 L540.0,245.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round" />
<circle cx="668.0" cy="181.0" r="3.2" fill="#0b0b0b" />
<circle cx="540.0" cy="245.0" r="3.2" fill="#0b0b0b" />
<path d="M668.0,181.0 L599.9,125.3 L652.0,189.0 L592.4,140.2 L638.0,196.0 L585.9,153.3 L625.8,202.1 L580.1,164.7 L615.1,207.5 L575.1,174.8 L605.7,212.1 L570.7,183.5 L597.5,216.3 L566.9,191.2 L590.3,219.8 L563.5,197.9 L584.0,223.0 L560.6,203.8 L578.5,225.7 L558.0,209.0 L573.7,228.1 L555.8,213.5 L569.5,230.2 L553.8,217.4 L565.8,232.1 L552.1,220.8 L562.6,233.7 L550.6,223.9 L559.8,235.1 L549.3,226.5 L557.3,236.3 L548.1,228.8 L555.1,237.4 L547.1,230.8 L553.3,238.4 L546.2,232.6 L551.6,239.2 L545.4,234.1 L550.2,239.9 L544.7,235.5" fill="none" stroke="#1baf7a" stroke-width="1.6" stroke-linejoin="round" stroke-dasharray="5 4" />
</g>
<circle cx="668.0" cy="181.0" r="4" fill="none" stroke="#0b0b0b" stroke-width="1.5" />
<text x="540.0" y="249.0" font-size="14" fill="#0b0b0b" text-anchor="middle">×</text>
<rect x="380" y="386" width="14" height="3" fill="#2a78d6" />
<text x="400" y="392" font-size="12" fill="#0b0b0b">梯度下降：208 步</text>
<rect x="380" y="403" width="14" height="3" fill="#eb6834" />
<text x="400" y="409" font-size="12" fill="#0b0b0b">Adam：219 步</text>
<rect x="380" y="420" width="14" height="3" fill="#0b0b0b" />
<text x="400" y="426" font-size="12" fill="#0b0b0b">Newton：1 步</text>
<rect x="380" y="437" width="14" height="3" fill="#1baf7a" />
<text x="400" y="443" font-size="12" fill="#0b0b0b">按曲率的对角缩放：208 步（与梯度下降重合）</text>
</svg>

<h2 id="想法一每个方向分别调步长">想法一：每个方向分别调步长</h2>

<p>很自然的想法是：横向陡，就横向走小步；顺向平，就顺向走大步。具体做法是给东西、南北两个方向各记一本账，按各自的弯曲程度缩放步长。</p>

<p>图 (a) 里山谷正好沿坐标轴摆放，绿色虚线就是这么做的。东西方向恰好是平的那个方向，南北方向恰好是陡的那个方向，分开缩放刚好对症：一步到底，和 Newton 法完全重合。</p>

<p>现在把同一个山谷转 45° 斜着放，见图 (b)。山的形状一点没变，只是转了个角度。往正东走，你会同时碰到陡的方向和平的方向；往正北走也一样。两本账记下来一模一样，按比例缩放等于没缩放。绿色虚线和梯度下降的蓝线完全重合，还是 208 步。</p>

<p><strong>按坐标轴分别调步长，只在山谷顺着坐标轴摆放时有用。</strong></p>

<h2 id="想法二看清山谷的形状">想法二：看清山谷的形状</h2>

<p>Newton 法不按东南西北记账。它摸清的是整个山谷的形状：长轴朝哪儿，短轴朝哪儿，各自弯得多厉害。知道了形状，谷底在哪儿就能直接算出来，一步走过去。</p>

<p>山谷怎么转都没关系，因为它看的是山谷本身，不是坐标轴。图里两种摆法，黑线都是一步到底。</p>

<h2 id="adam-在哪儿">Adam 在哪儿</h2>

<p>Adam 常被说成是想法一在深度学习里的实现：给每个参数单独一个步长。但它记的账和想法一不一样。想法一记的是”坡怎么弯”（曲率），Adam 记的是”坡有多陡”（梯度的大小）。</p>

<p>这两件事差别很大。一处坡很陡，可能是因为那里的墙本来就陡，也可能只是因为你离谷底还远。Adam 分不清这两种情况。而且越靠近谷底，坡越缓，它记的账也一直在变。</p>

<p>实测结果是：图里橙色的 Adam，学习率在 13 个值里挑了最好的，正着放要 213 步，斜着放要 219 步，两种都和梯度下降的 208 步差不多。正着放的山谷里，它并没有享受到想法一的一步到底。</p>

<p>这不说明 Adam 不好。这个例子只有两个变量、没有噪声，并不是 Adam 设计时针对的场合；它是为几十亿参数、梯度带噪声的训练设计的。这里只说明一件事：<strong>Adam 的”分别调步长”和按曲率调步长不是一回事，更不是 Newton。</strong></p>

<h2 id="那为什么深度学习不用-newton">那为什么深度学习不用 Newton？</h2>

<p>因为摸清山谷的形状太贵了。山谷的形状是一个矩阵（Hessian），有 n 个参数就有 n × n 个数。大模型有几十亿个参数，这个矩阵存都存不下，更别说求逆。而且每次只看一小批数据，脚下的感觉本身就是晃的，算出来的形状也不准。</p>

<p>物理里的自洽问题正好相反：变量只有几个到几百个，形状算得出来；没有噪声；还要求停得极准（残差 10⁻¹⁰）。这时花钱把形状摸清楚是值得的，所以用 Newton。</p>

<p>两者之间也有折中。深度学习里的 K-FAC、Shampoo 按层分块、用 Kronecker 乘积近似这个矩阵，比只记对角元多保留一部分方向之间的耦合；物理里的 Broyden、Anderson 混合则用历史步一点点拼出这个矩阵。</p>

<h2 id="数学">数学</h2>

<h3 id="同一个框架">同一个框架</h3>

<p>下面几种方法都可以写成</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x ← x − P ∇L(x)
</code></pre></div></div>

<p>区别只在预条件矩阵 P：</p>

<table>
  <thead>
    <tr>
      <th>方法</th>
      <th>P</th>
      <th>依据</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>梯度下降</td>
      <td>η I</td>
      <td>什么都不看，所有方向同一个步长</td>
    </tr>
    <tr>
      <td>按曲率的对角缩放</td>
      <td>η diag(H)⁻¹</td>
      <td>每个坐标上的曲率</td>
    </tr>
    <tr>
      <td>Adam</td>
      <td>η diag(1/(√v̂ + ε))，v̂ 是梯度平方的滑动平均</td>
      <td>每个坐标上梯度的大小</td>
    </tr>
    <tr>
      <td>Newton</td>
      <td>H⁻¹</td>
      <td>完整的曲率矩阵</td>
    </tr>
  </tbody>
</table>

<h3 id="例子">例子</h3>

<p>损失函数取二次型 L(x) = ½ xᵀ H x，谷底在 x = 0，</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>H = Q(θ) diag(0.05, 1.5) Q(θ)ᵀ,     条件数 κ = 1.5 / 0.05 = 30
</code></pre></div></div>

<table>
  <tbody>
    <tr>
      <td>Q(θ) 是转角为 θ 的旋转矩阵。图 (a) 是 θ = 0，图 (b) 是 θ = 45°。两种情况初值都是 x₀ = (1, 0.5)，”收敛”指</td>
      <td>x</td>
      <td>降到</td>
      <td>x₀</td>
      <td>的 10⁻⁶。</td>
    </tr>
  </tbody>
</table>

<p><strong>梯度下降。</strong> 在本征方向 i 上，误差每步乘以 1 − η λᵢ。两个因子绝对值相等时最好：η = 2/(λ_min + λ_max) = 2/1.55 ≈ 1.29，因子是 (κ − 1)/(κ + 1) = 29/31 ≈ 0.935，约需 ln 10⁻⁶ / ln 0.935 ≈ 208 步。这只和 H 的本征值有关，与 θ 无关，所以两种摆法都是 208 步。</p>

<p><strong>对角缩放。</strong> θ = 0 时 diag(H) = H，P = H⁻¹，就是 Newton，一步到底。θ = 45° 时</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>H = [[ 0.775, −0.725],
     [−0.725,  0.775]]
</code></pre></div></div>

<p>两个对角元都是 0.775，diag(H)⁻¹ 只是把单位矩阵乘了一个常数，等于换了个学习率的梯度下降，仍然是 208 步。非对角元 −0.725 才是”斜放”的信息，对角缩放把它丢掉了。</p>

<p><strong>Adam。</strong> 每步</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>m ← β₁ m + (1 − β₁) g          (梯度的滑动平均)
v ← β₂ v + (1 − β₂) g²         (梯度平方的滑动平均，逐坐标)
x ← x − η m̂ / (√v̂ + ε)          (m̂、v̂ 是去掉初始偏差后的值)
</code></pre></div></div>

<p>分母 √v̂ 衡量的是梯度的大小。在二次型里，坐标 i 上的梯度大小约等于曲率乘以那个方向上离谷底的距离，所以 Adam 的缩放把曲率和距离混在了一起，而距离一直在变。（这是对结果的解释，不是单独测出来的。）β₁ = 0.9、β₂ = 0.999、ε = 10⁻⁸，只调了学习率：在 10⁻³ 到 1 之间取 13 个等比的值，正放时最好的是 0.1（213 步），斜放时是 0.316（219 步）。</p>

<p><strong>带动量的梯度下降（heavy ball）。</strong> Adam 的 m 是一种动量，所以也比较一下纯动量的效果：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x_{k+1} = x_k − α ∇L(x_k) + β (x_k − x_{k−1})
</code></pre></div></div>

<p>取 Polyak 的最优参数 α = 4/(√λ_max + √λ_min)²、β = ((√κ − 1)/(√κ + 1))²，渐近因子从 (κ − 1)/(κ + 1) 改善到 (√κ − 1)/(√κ + 1) ≈ 0.691。实测正放 48 步、斜放 47 步，没有画在图里。动量相当于把条件数开了平方根，208 步变成约 48 步；但它和梯度下降一样，所有方向用同一个步长，所以山谷怎么摆都一样。</p>

<p><strong>Newton。</strong> P = H⁻¹，在二次型上一步就到谷底，与 θ 无关。</p>

<h3 id="实测步数">实测步数</h3>

<table>
  <thead>
    <tr>
      <th>方法</th>
      <th>正放 (θ = 0)</th>
      <th>斜放 (θ = 45°)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>梯度下降，η = 1.29</td>
      <td>208</td>
      <td>208</td>
    </tr>
    <tr>
      <td>按曲率的对角缩放</td>
      <td>1</td>
      <td>208</td>
    </tr>
    <tr>
      <td>Heavy ball（Polyak 参数）</td>
      <td>48</td>
      <td>47</td>
    </tr>
    <tr>
      <td>Adam（最好的学习率）</td>
      <td>213</td>
      <td>219</td>
    </tr>
    <tr>
      <td>Newton</td>
      <td>1</td>
      <td>1</td>
    </tr>
  </tbody>
</table>

<p>梯度下降、heavy ball 和 Newton 不依赖坐标轴，步数只取决于山谷本身的形状，所以转一下山谷步数不变。对角缩放和 Adam 依赖坐标轴，所以转一下就变了。区别在于：对角缩放从 1 步变成 208 步；Adam 本来就没占到便宜，两种都在 210 步左右。</p>

<h3 id="局限">局限</h3>

<ul>
  <li>只有两个变量、没有噪声、损失函数是严格的二次型、只试了一个初值。</li>
  <li>Adam 只调了学习率，β₁、β₂ 用默认值。</li>
  <li>这些结论说明的是几种预条件各自依据什么，不说明 Adam 训练神经网络的效果。</li>
</ul>

<h2 id="一句话">一句话</h2>

<p>Adam 和 Newton 都在”按方向调步长”。但 Adam 看的是坡有多陡，而且只按坐标轴分别看；Newton 看的是山谷的完整形状。山谷斜着放的时候，只有看形状的方法管用。</p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="optimization" /><summary type="html"><![CDATA[Adam 和 Newton 都想让陡的方向走小步、平的方向走大步。把同一个山谷正着放和斜着放，按曲率的对角缩放从 1 步变成 208 步，Newton 两种都是 1 步，Adam 两种都和梯度下降差不多。先看图，再看数学。]]></summary></entry><entry><title type="html">Adam and Newton both rescale the step. What’s the difference? A tilted valley</title><link href="https://thomaszhang2661.github.io/2026/11/26/adam-vs-newton/" rel="alternate" type="text/html" title="Adam and Newton both rescale the step. What’s the difference? A tilted valley" /><published>2026-11-26T00:00:00+00:00</published><updated>2026-11-26T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/26/adam-vs-newton</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/26/adam-vs-newton/"><![CDATA[<p><em>Tag: optimization. A follow-up to <a href="/2026/11/23/newton-gutzwiller-self-consistency/">the previous post on Newton’s method and self-consistent iteration</a>.</em></p>

<p><a href="/2026/11/26/adam-vs-newton-zh/">中文版</a></p>

<p>After the previous post, a natural question is: doesn’t Adam also rescale the step per direction? Isn’t that the
same idea as Newton’s method?</p>

<p>Half right. They want the same thing, but they are built from different parts. This post first builds the
intuition with one picture, then gives the math. Every number comes from one small two-variable example and was
measured, not estimated.</p>

<h2 id="a-long-narrow-valley">A long, narrow valley</h2>

<p>Imagine walking downhill blindfolded, trying to reach the bottom of a valley. All you can feel is which way the
ground slopes under your feet, and how steeply.</p>

<p>The simplest strategy is gradient descent: step in the downhill direction, with a fixed step length. The trouble is
that valleys are often long and narrow: steep across, nearly flat along. Take big steps and you bounce between the
two steep walls; take small steps and the walk along the flat floor takes forever.</p>

<p>The left panel below is such a valley. The curvature along the floor is 0.05 and across it 1.5, a factor of 30.
The blue line is gradient descent with the best possible step size. It still bounces for 208 steps before the error
falls to a millionth of where it started.</p>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 740 460" width="100%" role="img" aria-label="Same valley, two orientations" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Noto Sans SC', Helvetica, Arial, sans-serif;max-width:740px">
<rect width="740" height="460" fill="#fcfcfb" />
<text x="20" y="32" font-size="19" font-weight="600" fill="#0b0b0b">Same valley, two orientations</text>
<text x="20" y="54" font-size="12.5" fill="#52514e">contours of L = ½ xᵀHx with curvatures 0.05 and 1.5 (κ = 30); same start; first steps of each method</text>
<clipPath id="clip0"><rect x="40" y="120" width="320" height="250" /></clipPath>
<rect x="40" y="120" width="320" height="250" fill="#ffffff" stroke="#d6d5d0" />
<text x="40" y="108" font-size="13.5" font-weight="600" fill="#0b0b0b">(a) aligned with the axes</text>
<g clip-path="url(#clip0)">
<path d="M236.2,245.0 L236.2,244.7 L236.0,244.3 L235.8,244.0 L235.4,243.6 L235.0,243.3 L234.4,243.0 L233.8,242.6 L233.1,242.3 L232.3,242.0 L231.4,241.7 L230.4,241.4 L229.3,241.1 L228.1,240.8 L226.9,240.6 L225.6,240.3 L224.2,240.1 L222.8,239.9 L221.3,239.7 L219.7,239.5 L218.1,239.3 L216.4,239.1 L214.7,239.0 L213.0,238.8 L211.2,238.7 L209.4,238.6 L207.5,238.5 L205.7,238.5 L203.8,238.4 L201.9,238.4 L200.0,238.4 L198.1,238.4 L196.2,238.4 L194.3,238.5 L192.5,238.5 L190.6,238.6 L188.8,238.7 L187.0,238.8 L185.3,239.0 L183.6,239.1 L181.9,239.3 L180.3,239.5 L178.7,239.7 L177.2,239.9 L175.8,240.1 L174.4,240.3 L173.1,240.6 L171.9,240.8 L170.7,241.1 L169.6,241.4 L168.6,241.7 L167.7,242.0 L166.9,242.3 L166.2,242.6 L165.6,243.0 L165.0,243.3 L164.6,243.6 L164.2,244.0 L164.0,244.3 L163.8,244.7 L163.8,245.0 L163.8,245.3 L164.0,245.7 L164.2,246.0 L164.6,246.4 L165.0,246.7 L165.6,247.0 L166.2,247.4 L166.9,247.7 L167.7,248.0 L168.6,248.3 L169.6,248.6 L170.7,248.9 L171.9,249.2 L173.1,249.4 L174.4,249.7 L175.8,249.9 L177.2,250.1 L178.7,250.3 L180.3,250.5 L181.9,250.7 L183.6,250.9 L185.3,251.0 L187.0,251.2 L188.8,251.3 L190.6,251.4 L192.5,251.5 L194.3,251.5 L196.2,251.6 L198.1,251.6 L200.0,251.6 L201.9,251.6 L203.8,251.6 L205.7,251.5 L207.5,251.5 L209.4,251.4 L211.2,251.3 L213.0,251.2 L214.7,251.0 L216.4,250.9 L218.1,250.7 L219.7,250.5 L221.3,250.3 L222.8,250.1 L224.2,249.9 L225.6,249.7 L226.9,249.4 L228.1,249.2 L229.3,248.9 L230.4,248.6 L231.4,248.3 L232.3,248.0 L233.1,247.7 L233.8,247.4 L234.4,247.0 L235.0,246.7 L235.4,246.4 L235.8,246.0 L236.0,245.7 L236.2,245.3 L236.2,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M272.4,245.0 L272.3,244.3 L272.0,243.6 L271.5,242.9 L270.8,242.3 L269.9,241.6 L268.9,240.9 L267.6,240.3 L266.1,239.6 L264.5,239.0 L262.7,238.4 L260.7,237.8 L258.6,237.2 L256.3,236.7 L253.8,236.2 L251.2,235.7 L248.5,235.2 L245.6,234.7 L242.6,234.3 L239.4,233.9 L236.2,233.6 L232.9,233.2 L229.5,232.9 L225.9,232.7 L222.4,232.4 L218.7,232.2 L215.1,232.1 L211.3,231.9 L207.6,231.9 L203.8,231.8 L200.0,231.8 L196.2,231.8 L192.4,231.9 L188.7,231.9 L184.9,232.1 L181.3,232.2 L177.6,232.4 L174.1,232.7 L170.5,232.9 L167.1,233.2 L163.8,233.6 L160.6,233.9 L157.4,234.3 L154.4,234.7 L151.5,235.2 L148.8,235.7 L146.2,236.2 L143.7,236.7 L141.4,237.2 L139.3,237.8 L137.3,238.4 L135.5,239.0 L133.9,239.6 L132.4,240.3 L131.1,240.9 L130.1,241.6 L129.2,242.3 L128.5,242.9 L128.0,243.6 L127.7,244.3 L127.6,245.0 L127.7,245.7 L128.0,246.4 L128.5,247.1 L129.2,247.7 L130.1,248.4 L131.1,249.1 L132.4,249.7 L133.9,250.4 L135.5,251.0 L137.3,251.6 L139.3,252.2 L141.4,252.8 L143.7,253.3 L146.2,253.8 L148.8,254.3 L151.5,254.8 L154.4,255.3 L157.4,255.7 L160.6,256.1 L163.8,256.4 L167.1,256.8 L170.5,257.1 L174.1,257.3 L177.6,257.6 L181.3,257.8 L184.9,257.9 L188.7,258.1 L192.4,258.1 L196.2,258.2 L200.0,258.2 L203.8,258.2 L207.6,258.1 L211.3,258.1 L215.1,257.9 L218.7,257.8 L222.4,257.6 L225.9,257.3 L229.5,257.1 L232.9,256.8 L236.2,256.4 L239.4,256.1 L242.6,255.7 L245.6,255.3 L248.5,254.8 L251.2,254.3 L253.8,253.8 L256.3,253.3 L258.6,252.8 L260.7,252.2 L262.7,251.6 L264.5,251.0 L266.1,250.4 L267.6,249.7 L268.9,249.1 L269.9,248.4 L270.8,247.7 L271.5,247.1 L272.0,246.4 L272.3,245.7 L272.4,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M314.5,245.0 L314.3,243.9 L313.9,242.8 L313.1,241.7 L312.0,240.7 L310.6,239.6 L308.9,238.5 L306.9,237.5 L304.6,236.5 L302.0,235.5 L299.1,234.5 L296.0,233.6 L292.6,232.7 L289.0,231.8 L285.1,231.0 L281.0,230.2 L276.6,229.5 L272.0,228.8 L267.3,228.1 L262.4,227.5 L257.2,226.9 L252.0,226.4 L246.6,225.9 L241.0,225.5 L235.4,225.1 L229.6,224.8 L223.8,224.6 L217.9,224.4 L212.0,224.2 L206.0,224.1 L200.0,224.1 L194.0,224.1 L188.0,224.2 L182.1,224.4 L176.2,224.6 L170.4,224.8 L164.6,225.1 L159.0,225.5 L153.4,225.9 L148.0,226.4 L142.8,226.9 L137.6,227.5 L132.7,228.1 L128.0,228.8 L123.4,229.5 L119.0,230.2 L114.9,231.0 L111.0,231.8 L107.4,232.7 L104.0,233.6 L100.9,234.5 L98.0,235.5 L95.4,236.5 L93.1,237.5 L91.1,238.5 L89.4,239.6 L88.0,240.7 L86.9,241.7 L86.1,242.8 L85.7,243.9 L85.5,245.0 L85.7,246.1 L86.1,247.2 L86.9,248.3 L88.0,249.3 L89.4,250.4 L91.1,251.5 L93.1,252.5 L95.4,253.5 L98.0,254.5 L100.9,255.5 L104.0,256.4 L107.4,257.3 L111.0,258.2 L114.9,259.0 L119.0,259.8 L123.4,260.5 L128.0,261.2 L132.7,261.9 L137.6,262.5 L142.8,263.1 L148.0,263.6 L153.4,264.1 L159.0,264.5 L164.6,264.9 L170.4,265.2 L176.2,265.4 L182.1,265.6 L188.0,265.8 L194.0,265.9 L200.0,265.9 L206.0,265.9 L212.0,265.8 L217.9,265.6 L223.8,265.4 L229.6,265.2 L235.4,264.9 L241.0,264.5 L246.6,264.1 L252.0,263.6 L257.2,263.1 L262.4,262.5 L267.3,261.9 L272.0,261.2 L276.6,260.5 L281.0,259.8 L285.1,259.0 L289.0,258.2 L292.6,257.3 L296.0,256.4 L299.1,255.5 L302.0,254.5 L304.6,253.5 L306.9,252.5 L308.9,251.5 L310.6,250.4 L312.0,249.3 L313.1,248.3 L313.9,247.2 L314.3,246.1 L314.5,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M381.0,245.0 L380.8,243.3 L380.0,241.5 L378.8,239.8 L377.1,238.1 L374.9,236.4 L372.2,234.8 L369.0,233.2 L365.4,231.6 L361.3,230.0 L356.8,228.5 L351.8,227.0 L346.4,225.6 L340.7,224.2 L334.5,222.9 L328.0,221.6 L321.1,220.4 L313.9,219.3 L306.4,218.3 L298.6,217.3 L290.5,216.4 L282.2,215.6 L273.6,214.8 L264.9,214.1 L255.9,213.6 L246.9,213.1 L237.6,212.7 L228.3,212.4 L218.9,212.1 L209.5,212.0 L200.0,212.0 L190.5,212.0 L181.1,212.1 L171.7,212.4 L162.4,212.7 L153.1,213.1 L144.1,213.6 L135.1,214.1 L126.4,214.8 L117.8,215.6 L109.5,216.4 L101.4,217.3 L93.6,218.3 L86.1,219.3 L78.9,220.4 L72.0,221.6 L65.5,222.9 L59.3,224.2 L53.6,225.6 L48.2,227.0 L43.2,228.5 L38.7,230.0 L34.6,231.6 L31.0,233.2 L27.8,234.8 L25.1,236.4 L22.9,238.1 L21.2,239.8 L20.0,241.5 L19.2,243.3 L19.0,245.0 L19.2,246.7 L20.0,248.5 L21.2,250.2 L22.9,251.9 L25.1,253.6 L27.8,255.2 L31.0,256.8 L34.6,258.4 L38.7,260.0 L43.2,261.5 L48.2,263.0 L53.6,264.4 L59.3,265.8 L65.5,267.1 L72.0,268.4 L78.9,269.6 L86.1,270.7 L93.6,271.7 L101.4,272.7 L109.5,273.6 L117.8,274.4 L126.4,275.2 L135.1,275.9 L144.1,276.4 L153.1,276.9 L162.4,277.3 L171.7,277.6 L181.1,277.9 L190.5,278.0 L200.0,278.0 L209.5,278.0 L218.9,277.9 L228.3,277.6 L237.6,277.3 L246.9,276.9 L255.9,276.4 L264.9,275.9 L273.6,275.2 L282.2,274.4 L290.5,273.6 L298.6,272.7 L306.4,271.7 L313.9,270.7 L321.1,269.6 L328.0,268.4 L334.5,267.1 L340.7,265.8 L346.4,264.4 L351.8,263.0 L356.8,261.5 L361.3,260.0 L365.4,258.4 L369.0,256.8 L372.2,255.2 L374.9,253.6 L377.1,251.9 L378.8,250.2 L380.0,248.5 L380.8,246.7 L381.0,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M456.0,245.0 L455.6,242.6 L454.6,240.1 L452.8,237.7 L450.4,235.3 L447.3,232.9 L443.5,230.6 L439.0,228.3 L433.9,226.0 L428.1,223.8 L421.7,221.6 L414.7,219.5 L407.1,217.5 L398.9,215.6 L390.2,213.7 L381.0,212.0 L371.3,210.3 L361.1,208.7 L350.5,207.2 L339.4,205.8 L328.0,204.5 L316.2,203.4 L304.1,202.3 L291.7,201.4 L279.1,200.5 L266.3,199.9 L253.2,199.3 L240.0,198.8 L226.8,198.5 L213.4,198.3 L200.0,198.3 L186.6,198.3 L173.2,198.5 L160.0,198.8 L146.8,199.3 L133.7,199.9 L120.9,200.5 L108.3,201.4 L95.9,202.3 L83.8,203.4 L72.0,204.5 L60.6,205.8 L49.5,207.2 L38.9,208.7 L28.7,210.3 L19.0,212.0 L9.8,213.7 L1.1,215.6 L-7.1,217.5 L-14.7,219.5 L-21.7,221.6 L-28.1,223.8 L-33.9,226.0 L-39.0,228.3 L-43.5,230.6 L-47.3,232.9 L-50.4,235.3 L-52.8,237.7 L-54.6,240.1 L-55.6,242.6 L-56.0,245.0 L-55.6,247.4 L-54.6,249.9 L-52.8,252.3 L-50.4,254.7 L-47.3,257.1 L-43.5,259.4 L-39.0,261.7 L-33.9,264.0 L-28.1,266.2 L-21.7,268.4 L-14.7,270.5 L-7.1,272.5 L1.1,274.4 L9.8,276.3 L19.0,278.0 L28.7,279.7 L38.9,281.3 L49.5,282.8 L60.6,284.2 L72.0,285.5 L83.8,286.6 L95.9,287.7 L108.3,288.6 L120.9,289.5 L133.7,290.1 L146.8,290.7 L160.0,291.2 L173.2,291.5 L186.6,291.7 L200.0,291.7 L213.4,291.7 L226.8,291.5 L240.0,291.2 L253.2,290.7 L266.3,290.1 L279.1,289.5 L291.7,288.6 L304.1,287.7 L316.2,286.6 L328.0,285.5 L339.4,284.2 L350.5,282.8 L361.1,281.3 L371.3,279.7 L381.0,278.0 L390.2,276.3 L398.9,274.4 L407.1,272.5 L414.7,270.5 L421.7,268.4 L428.1,266.2 L433.9,264.0 L439.0,261.7 L443.5,259.4 L447.3,257.1 L450.4,254.7 L452.8,252.3 L454.6,249.9 L455.6,247.4 L456.0,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M562.0,245.0 L561.5,241.5 L560.1,238.1 L557.6,234.7 L554.1,231.3 L549.7,227.9 L544.3,224.6 L538.0,221.3 L530.7,218.1 L522.6,215.0 L513.5,212.0 L503.6,209.0 L492.9,206.1 L481.4,203.4 L469.0,200.8 L456.0,198.3 L442.3,195.9 L427.8,193.6 L412.8,191.5 L397.2,189.6 L381.0,187.8 L364.4,186.1 L347.3,184.6 L329.7,183.3 L311.9,182.1 L293.7,181.2 L275.3,180.3 L256.6,179.7 L237.8,179.3 L218.9,179.0 L200.0,178.9 L181.1,179.0 L162.2,179.3 L143.4,179.7 L124.7,180.3 L106.3,181.2 L88.1,182.1 L70.3,183.3 L52.7,184.6 L35.6,186.1 L19.0,187.8 L2.8,189.6 L-12.8,191.5 L-27.8,193.6 L-42.3,195.9 L-56.0,198.3 L-69.0,200.8 L-81.4,203.4 L-92.9,206.1 L-103.6,209.0 L-113.5,212.0 L-122.6,215.0 L-130.7,218.1 L-138.0,221.3 L-144.3,224.6 L-149.7,227.9 L-154.1,231.3 L-157.6,234.7 L-160.1,238.1 L-161.5,241.5 L-162.0,245.0 L-161.5,248.5 L-160.1,251.9 L-157.6,255.3 L-154.1,258.7 L-149.7,262.1 L-144.3,265.4 L-138.0,268.7 L-130.7,271.9 L-122.6,275.0 L-113.5,278.0 L-103.6,281.0 L-92.9,283.9 L-81.4,286.6 L-69.0,289.2 L-56.0,291.7 L-42.3,294.1 L-27.8,296.4 L-12.8,298.5 L2.8,300.4 L19.0,302.2 L35.6,303.9 L52.7,305.4 L70.3,306.7 L88.1,307.9 L106.3,308.8 L124.7,309.7 L143.4,310.3 L162.2,310.7 L181.1,311.0 L200.0,311.1 L218.9,311.0 L237.8,310.7 L256.6,310.3 L275.3,309.7 L293.7,308.8 L311.9,307.9 L329.7,306.7 L347.3,305.4 L364.4,303.9 L381.0,302.2 L397.2,300.4 L412.8,298.5 L427.8,296.4 L442.3,294.1 L456.0,291.7 L469.0,289.2 L481.4,286.6 L492.9,283.9 L503.6,281.0 L513.5,278.0 L522.6,275.0 L530.7,271.9 L538.0,268.7 L544.3,265.4 L549.7,262.1 L554.1,258.7 L557.6,255.3 L560.1,251.9 L561.5,248.5 L562.0,245.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M328.0,181.0 L319.7,304.9 L312.0,189.0 L304.8,297.4 L298.0,196.0 L291.7,290.9 L285.8,202.1 L280.3,285.1 L275.1,207.5 L270.2,280.1 L265.7,212.1 L261.5,275.7 L257.5,216.3 L253.8,271.9 L250.3,219.8 L247.1,268.5 L244.0,223.0 L241.2,265.6 L238.5,225.7 L236.0,263.0 L233.7,228.1 L231.5,260.8 L229.5,230.2 L227.6,258.8 L225.8,232.1 L224.2,257.1 L222.6,233.7 L221.1,255.6 L219.8,235.1 L218.5,254.3 L217.3,236.3 L216.2,253.1 L215.1,237.4 L214.2,252.1 L213.3,238.4 L212.4,251.2 L211.6,239.2 L210.9,250.4 L210.2,239.9 L209.5,249.7" fill="none" stroke="#2a78d6" stroke-width="1.6" stroke-linejoin="round" />
<path d="M328.0,181.0 L315.2,193.8 L302.5,206.4 L289.8,218.8 L277.3,230.5 L265.0,241.4 L253.0,251.1 L241.4,259.1 L230.2,265.1 L219.7,269.1 L209.8,271.0 L200.7,271.2 L192.5,269.8 L185.3,267.3 L179.1,263.8 L174.1,259.6 L170.2,255.1 L167.4,250.5 L165.7,245.9 L165.0,241.8 L165.3,238.2 L166.4,235.3 L168.2,233.3 L170.6,232.1 L173.6,231.7 L176.9,232.2 L180.5,233.4 L184.3,235.1 L188.1,237.3 L192.0,239.8 L195.7,242.3 L199.1,244.8 L202.3,247.1 L205.2,249.0 L207.6,250.5 L209.6,251.5 L211.2,252.0 L212.3,252.0 L212.9,251.6 L213.1,250.7 L212.9,249.5 L212.3,248.1 L211.4,246.7 L210.2,245.2 L208.9,243.9 L207.3,242.7 L205.7,241.9 L204.0,241.3 L202.4,241.1 L200.8,241.1 L199.4,241.5 L198.1,242.1 L197.0,242.9 L196.1,243.7 L195.4,244.6 L194.9,245.4 L194.6,246.1 L194.6,246.7 L194.8,247.1 L195.1,247.2 L195.6,247.2 L196.2,247.0 L196.8,246.6 L197.6,246.2 L198.3,245.7 L199.1,245.1 L199.8,244.6 L200.4,244.2 L201.0,243.9 L201.5,243.7 L201.9,243.7 L202.1,243.7 L202.3,243.9 L202.4,244.2 L202.3,244.5 L202.2,244.8 L202.0,245.1 L201.7,245.4 L201.4,245.6 L201.0,245.7 L200.7,245.8 L200.3,245.8 L200.0,245.7 L199.7,245.5 L199.4,245.3 L199.2,245.1 L199.1,244.9 L199.0,244.8 L198.9,244.6 L198.9,244.6 L199.0,244.5 L199.1,244.6 L199.2,244.6 L199.3,244.7 L199.5,244.8 L199.7,245.0 L199.8,245.1 L200.0,245.2 L200.2,245.2 L200.3,245.3 L200.4,245.3 L200.4,245.3 L200.5,245.2 L200.5,245.1 L200.5,245.1 L200.5,245.0 L200.4,244.9 L200.3,244.9 L200.3,244.8 L200.2,244.8 L200.1,244.8 L200.0,244.9 L199.9,244.9 L199.9,245.0 L199.8,245.0 L199.8,245.0 L199.8,245.1 L199.8,245.1 L199.8,245.1 L199.8,245.1" fill="none" stroke="#eb6834" stroke-width="1.6" stroke-linejoin="round" />
<path d="M328.0,181.0 L200.0,245.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round" />
<circle cx="328.0" cy="181.0" r="3.2" fill="#0b0b0b" />
<circle cx="200.0" cy="245.0" r="3.2" fill="#0b0b0b" />
<path d="M328.0,181.0 L200.0,245.0" fill="none" stroke="#1baf7a" stroke-width="1.6" stroke-linejoin="round" stroke-dasharray="5 4" />
</g>
<circle cx="328.0" cy="181.0" r="4" fill="none" stroke="#0b0b0b" stroke-width="1.5" />
<text x="200.0" y="249.0" font-size="14" fill="#0b0b0b" text-anchor="middle">×</text>
<rect x="40" y="386" width="14" height="3" fill="#2a78d6" />
<text x="60" y="392" font-size="12" fill="#0b0b0b">gradient descent: 208 steps</text>
<rect x="40" y="403" width="14" height="3" fill="#eb6834" />
<text x="60" y="409" font-size="12" fill="#0b0b0b">Adam: 213 steps</text>
<rect x="40" y="420" width="14" height="3" fill="#0b0b0b" />
<text x="60" y="426" font-size="12" fill="#0b0b0b">Newton: 1 step</text>
<rect x="40" y="437" width="14" height="3" fill="#1baf7a" />
<text x="60" y="443" font-size="12" fill="#0b0b0b">diagonal scaling by curvature: 1 step (on top of Newton)</text>
<clipPath id="clip1"><rect x="380" y="120" width="320" height="250" /></clipPath>
<rect x="380" y="120" width="320" height="250" fill="#ffffff" stroke="#d6d5d0" />
<text x="380" y="108" font-size="13.5" font-weight="600" fill="#0b0b0b">(b) tilted by 45°</text>
<g clip-path="url(#clip1)">
<path d="M565.6,219.4 L565.3,219.2 L565.0,219.1 L564.6,219.0 L564.1,219.0 L563.5,219.1 L562.9,219.2 L562.2,219.4 L561.5,219.7 L560.7,220.1 L559.8,220.5 L558.9,221.0 L558.0,221.5 L557.0,222.2 L555.9,222.8 L554.8,223.6 L553.7,224.4 L552.5,225.3 L551.3,226.2 L550.0,227.1 L548.8,228.2 L547.5,229.2 L546.1,230.3 L544.8,231.5 L543.5,232.6 L542.1,233.9 L540.8,235.1 L539.4,236.4 L538.0,237.7 L536.7,239.0 L535.3,240.3 L534.0,241.7 L532.7,243.0 L531.4,244.4 L530.1,245.8 L528.9,247.1 L527.6,248.5 L526.5,249.8 L525.3,251.1 L524.2,252.5 L523.2,253.8 L522.1,255.0 L521.2,256.3 L520.3,257.5 L519.4,258.7 L518.6,259.8 L517.8,260.9 L517.2,262.0 L516.5,263.0 L516.0,263.9 L515.5,264.8 L515.1,265.7 L514.7,266.5 L514.4,267.2 L514.2,267.9 L514.1,268.5 L514.0,269.1 L514.0,269.6 L514.1,270.0 L514.2,270.3 L514.4,270.6 L514.7,270.8 L515.0,270.9 L515.4,271.0 L515.9,271.0 L516.5,270.9 L517.1,270.8 L517.8,270.6 L518.5,270.3 L519.3,269.9 L520.2,269.5 L521.1,269.0 L522.0,268.5 L523.0,267.8 L524.1,267.2 L525.2,266.4 L526.3,265.6 L527.5,264.7 L528.7,263.8 L530.0,262.9 L531.2,261.8 L532.5,260.8 L533.9,259.7 L535.2,258.5 L536.5,257.4 L537.9,256.1 L539.2,254.9 L540.6,253.6 L542.0,252.3 L543.3,251.0 L544.7,249.7 L546.0,248.3 L547.3,247.0 L548.6,245.6 L549.9,244.2 L551.1,242.9 L552.4,241.5 L553.5,240.2 L554.7,238.9 L555.8,237.5 L556.8,236.2 L557.9,235.0 L558.8,233.7 L559.7,232.5 L560.6,231.3 L561.4,230.2 L562.2,229.1 L562.8,228.0 L563.5,227.0 L564.0,226.1 L564.5,225.2 L564.9,224.3 L565.3,223.5 L565.6,222.8 L565.8,222.1 L565.9,221.5 L566.0,220.9 L566.0,220.4 L565.9,220.0 L565.8,219.7 L565.6,219.4" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M591.2,193.8 L590.6,193.4 L589.9,193.1 L589.1,193.0 L588.1,193.0 L587.0,193.1 L585.8,193.4 L584.4,193.9 L583.0,194.4 L581.4,195.1 L579.7,196.0 L577.8,197.0 L575.9,198.1 L573.9,199.3 L571.8,200.7 L569.6,202.2 L567.3,203.8 L565.0,205.5 L562.5,207.3 L560.0,209.3 L557.5,211.3 L554.9,213.4 L552.3,215.6 L549.6,217.9 L546.9,220.3 L544.2,222.7 L541.5,225.2 L538.8,227.8 L536.1,230.4 L533.3,233.0 L530.7,235.7 L528.0,238.3 L525.4,241.1 L522.8,243.8 L520.2,246.5 L517.7,249.2 L515.3,251.9 L512.9,254.6 L510.6,257.3 L508.4,259.9 L506.3,262.5 L504.3,265.0 L502.3,267.5 L500.5,270.0 L498.8,272.3 L497.2,274.6 L495.7,276.8 L494.3,278.9 L493.1,280.9 L492.0,282.8 L491.0,284.7 L490.1,286.4 L489.4,288.0 L488.9,289.4 L488.4,290.8 L488.1,292.0 L488.0,293.1 L488.0,294.1 L488.1,294.9 L488.4,295.6 L488.8,296.2 L489.4,296.6 L490.1,296.9 L490.9,297.0 L491.9,297.0 L493.0,296.9 L494.2,296.6 L495.6,296.1 L497.0,295.6 L498.6,294.9 L500.3,294.0 L502.2,293.0 L504.1,291.9 L506.1,290.7 L508.2,289.3 L510.4,287.8 L512.7,286.2 L515.0,284.5 L517.5,282.7 L520.0,280.7 L522.5,278.7 L525.1,276.6 L527.7,274.4 L530.4,272.1 L533.1,269.7 L535.8,267.3 L538.5,264.8 L541.2,262.2 L543.9,259.6 L546.7,257.0 L549.3,254.3 L552.0,251.7 L554.6,248.9 L557.2,246.2 L559.8,243.5 L562.3,240.8 L564.7,238.1 L567.1,235.4 L569.4,232.7 L571.6,230.1 L573.7,227.5 L575.7,225.0 L577.7,222.5 L579.5,220.0 L581.2,217.7 L582.8,215.4 L584.3,213.2 L585.7,211.1 L586.9,209.1 L588.0,207.2 L589.0,205.3 L589.9,203.6 L590.6,202.0 L591.1,200.6 L591.6,199.2 L591.9,198.0 L592.0,196.9 L592.0,195.9 L591.9,195.1 L591.6,194.4 L591.2,193.8" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M621.0,164.0 L620.1,163.4 L619.0,162.9 L617.6,162.7 L616.1,162.7 L614.4,163.0 L612.4,163.4 L610.3,164.1 L607.9,165.0 L605.4,166.2 L602.7,167.5 L599.8,169.1 L596.8,170.8 L593.6,172.8 L590.3,174.9 L586.8,177.3 L583.2,179.8 L579.5,182.6 L575.6,185.5 L571.7,188.5 L567.7,191.7 L563.6,195.1 L559.4,198.6 L555.2,202.2 L551.0,205.9 L546.7,209.8 L542.4,213.7 L538.1,217.7 L533.8,221.8 L529.5,226.0 L525.2,230.2 L521.0,234.5 L516.8,238.8 L512.7,243.1 L508.7,247.4 L504.8,251.7 L500.9,256.0 L497.2,260.2 L493.6,264.4 L490.1,268.6 L486.7,272.7 L483.5,276.7 L480.5,280.6 L477.6,284.5 L474.8,288.2 L472.3,291.8 L469.9,295.3 L467.8,298.6 L465.8,301.8 L464.1,304.8 L462.5,307.7 L461.2,310.4 L460.0,312.9 L459.1,315.3 L458.4,317.4 L458.0,319.4 L457.7,321.1 L457.7,322.6 L457.9,324.0 L458.4,325.1 L459.0,326.0 L459.9,326.6 L461.0,327.1 L462.4,327.3 L463.9,327.3 L465.6,327.0 L467.6,326.6 L469.7,325.9 L472.1,325.0 L474.6,323.8 L477.3,322.5 L480.2,320.9 L483.2,319.2 L486.4,317.2 L489.7,315.1 L493.2,312.7 L496.8,310.2 L500.5,307.4 L504.4,304.5 L508.3,301.5 L512.3,298.3 L516.4,294.9 L520.6,291.4 L524.8,287.8 L529.0,284.1 L533.3,280.2 L537.6,276.3 L541.9,272.3 L546.2,268.2 L550.5,264.0 L554.8,259.8 L559.0,255.5 L563.2,251.2 L567.3,246.9 L571.3,242.6 L575.2,238.3 L579.1,234.0 L582.8,229.8 L586.4,225.6 L589.9,221.4 L593.3,217.3 L596.5,213.3 L599.5,209.4 L602.4,205.5 L605.2,201.8 L607.7,198.2 L610.1,194.7 L612.2,191.4 L614.2,188.2 L615.9,185.2 L617.5,182.3 L618.8,179.6 L620.0,177.1 L620.9,174.7 L621.6,172.6 L622.0,170.6 L622.3,168.9 L622.3,167.4 L622.1,166.0 L621.6,164.9 L621.0,164.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M668.0,117.0 L666.6,116.0 L664.9,115.3 L662.8,114.9 L660.3,114.9 L657.6,115.3 L654.5,116.0 L651.1,117.1 L647.4,118.6 L643.4,120.3 L639.2,122.5 L634.6,124.9 L629.8,127.7 L624.8,130.8 L619.5,134.2 L614.0,138.0 L608.3,142.0 L602.4,146.3 L596.3,150.9 L590.1,155.7 L583.8,160.8 L577.3,166.1 L570.7,171.6 L564.1,177.3 L557.3,183.2 L550.6,189.3 L543.8,195.5 L536.9,201.9 L530.1,208.4 L523.4,215.0 L516.6,221.6 L510.0,228.4 L503.4,235.1 L496.9,241.9 L490.5,248.8 L484.3,255.6 L478.2,262.3 L472.3,269.1 L466.6,275.7 L461.1,282.3 L455.8,288.8 L450.7,295.1 L445.9,301.3 L441.3,307.4 L437.0,313.3 L433.0,319.0 L429.2,324.5 L425.8,329.8 L422.7,334.8 L419.9,339.6 L417.5,344.2 L415.3,348.4 L413.6,352.4 L412.1,356.1 L411.0,359.5 L410.3,362.6 L409.9,365.3 L409.9,367.8 L410.3,369.9 L411.0,371.6 L412.0,373.0 L413.4,374.0 L415.1,374.7 L417.2,375.1 L419.7,375.1 L422.4,374.7 L425.5,374.0 L428.9,372.9 L432.6,371.4 L436.6,369.7 L440.8,367.5 L445.4,365.1 L450.2,362.3 L455.2,359.2 L460.5,355.8 L466.0,352.0 L471.7,348.0 L477.6,343.7 L483.7,339.1 L489.9,334.3 L496.2,329.2 L502.7,323.9 L509.3,318.4 L515.9,312.7 L522.7,306.8 L529.4,300.7 L536.2,294.5 L543.1,288.1 L549.9,281.6 L556.6,275.0 L563.4,268.4 L570.0,261.6 L576.6,254.9 L583.1,248.1 L589.5,241.2 L595.7,234.4 L601.8,227.7 L607.7,220.9 L613.4,214.3 L618.9,207.7 L624.2,201.2 L629.3,194.9 L634.1,188.7 L638.7,182.6 L643.0,176.7 L647.0,171.0 L650.8,165.5 L654.2,160.2 L657.3,155.2 L660.1,150.4 L662.5,145.8 L664.7,141.6 L666.4,137.6 L667.9,133.9 L669.0,130.5 L669.7,127.4 L670.1,124.7 L670.1,122.2 L669.7,120.1 L669.0,118.4 L668.0,117.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M721.0,64.0 L719.0,62.5 L716.6,61.5 L713.6,61.0 L710.2,61.1 L706.3,61.6 L701.9,62.6 L697.2,64.2 L691.9,66.2 L686.3,68.7 L680.2,71.7 L673.8,75.2 L667.0,79.1 L659.9,83.5 L652.4,88.4 L644.6,93.6 L636.6,99.3 L628.2,105.4 L619.7,111.9 L610.9,118.7 L601.9,125.9 L592.7,133.4 L583.4,141.2 L574.0,149.3 L564.5,157.6 L554.9,166.2 L545.3,175.0 L535.7,184.0 L526.1,193.2 L516.5,202.5 L507.0,212.0 L497.5,221.5 L488.2,231.1 L479.0,240.7 L470.0,250.3 L461.2,259.9 L452.6,269.5 L444.3,279.0 L436.2,288.4 L428.4,297.7 L420.9,306.9 L413.7,315.9 L406.9,324.7 L400.4,333.2 L394.3,341.6 L388.6,349.6 L383.4,357.4 L378.5,364.9 L374.1,372.0 L370.2,378.8 L366.7,385.2 L363.7,391.3 L361.2,396.9 L359.2,402.2 L357.6,406.9 L356.6,411.3 L356.1,415.2 L356.0,418.6 L356.5,421.6 L357.5,424.0 L359.0,426.0 L361.0,427.5 L363.4,428.5 L366.4,429.0 L369.8,428.9 L373.7,428.4 L378.1,427.4 L382.8,425.8 L388.1,423.8 L393.7,421.3 L399.8,418.3 L406.2,414.8 L413.0,410.9 L420.1,406.5 L427.6,401.6 L435.4,396.4 L443.4,390.7 L451.8,384.6 L460.3,378.1 L469.1,371.3 L478.1,364.1 L487.3,356.6 L496.6,348.8 L506.0,340.7 L515.5,332.4 L525.1,323.8 L534.7,315.0 L544.3,306.0 L553.9,296.8 L563.5,287.5 L573.0,278.0 L582.5,268.5 L591.8,258.9 L601.0,249.3 L610.0,239.7 L618.8,230.1 L627.4,220.5 L635.7,211.0 L643.8,201.6 L651.6,192.3 L659.1,183.1 L666.3,174.1 L673.1,165.3 L679.6,156.8 L685.7,148.4 L691.4,140.4 L696.6,132.6 L701.5,125.1 L705.9,118.0 L709.8,111.2 L713.3,104.8 L716.3,98.7 L718.8,93.1 L720.8,87.8 L722.4,83.1 L723.4,78.7 L723.9,74.8 L724.0,71.4 L723.5,68.4 L722.5,66.0 L721.0,64.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M796.0,-11.0 L793.2,-13.1 L789.7,-14.5 L785.5,-15.2 L780.7,-15.1 L775.2,-14.4 L769.0,-12.9 L762.2,-10.7 L754.9,-7.9 L746.9,-4.3 L738.3,-0.1 L729.2,4.8 L719.6,10.4 L709.5,16.6 L699.0,23.5 L688.0,30.9 L676.6,39.0 L664.8,47.6 L652.7,56.7 L640.2,66.4 L627.5,76.5 L614.6,87.1 L601.4,98.2 L588.1,109.6 L574.7,121.4 L561.1,133.6 L547.5,146.1 L533.9,158.8 L520.3,171.8 L506.7,184.9 L493.3,198.3 L479.9,211.7 L466.8,225.3 L453.8,238.9 L441.1,252.5 L428.6,266.1 L416.4,279.7 L404.6,293.1 L393.2,306.4 L382.1,319.6 L371.5,332.5 L361.4,345.2 L351.7,357.7 L342.6,369.8 L334.0,381.6 L325.9,393.0 L318.5,404.0 L311.6,414.5 L305.4,424.6 L299.8,434.2 L294.9,443.3 L290.7,451.9 L287.1,459.9 L284.3,467.2 L282.1,474.0 L280.6,480.2 L279.9,485.7 L279.8,490.5 L280.5,494.7 L281.9,498.2 L284.0,501.0 L286.8,503.1 L290.3,504.5 L294.5,505.2 L299.3,505.1 L304.8,504.4 L311.0,502.9 L317.8,500.7 L325.1,497.9 L333.1,494.3 L341.7,490.1 L350.8,485.2 L360.4,479.6 L370.5,473.4 L381.0,466.5 L392.0,459.1 L403.4,451.0 L415.2,442.4 L427.3,433.3 L439.8,423.6 L452.5,413.5 L465.4,402.9 L478.6,391.8 L491.9,380.4 L505.3,368.6 L518.9,356.4 L532.5,343.9 L546.1,331.2 L559.7,318.2 L573.3,305.1 L586.7,291.7 L600.1,278.3 L613.2,264.7 L626.2,251.1 L638.9,237.5 L651.4,223.9 L663.6,210.3 L675.4,196.9 L686.8,183.6 L697.9,170.4 L708.5,157.5 L718.6,144.8 L728.3,132.3 L737.4,120.2 L746.0,108.4 L754.1,97.0 L761.5,86.0 L768.4,75.5 L774.6,65.4 L780.2,55.8 L785.1,46.7 L789.3,38.1 L792.9,30.1 L795.7,22.8 L797.9,16.0 L799.4,9.8 L800.1,4.3 L800.2,-0.5 L799.5,-4.7 L798.1,-8.2 L796.0,-11.0" fill="none" stroke="#e4e3df" stroke-width="1" />
<path d="M668.0,181.0 L599.9,125.3 L652.0,189.0 L592.4,140.2 L638.0,196.0 L585.9,153.3 L625.8,202.1 L580.1,164.7 L615.1,207.5 L575.1,174.8 L605.7,212.1 L570.7,183.5 L597.5,216.3 L566.9,191.2 L590.3,219.8 L563.5,197.9 L584.0,223.0 L560.6,203.8 L578.5,225.7 L558.0,209.0 L573.7,228.1 L555.8,213.5 L569.5,230.2 L553.8,217.4 L565.8,232.1 L552.1,220.8 L562.6,233.7 L550.6,223.9 L559.8,235.1 L549.3,226.5 L557.3,236.3 L548.1,228.8 L555.1,237.4 L547.1,230.8 L553.3,238.4 L546.2,232.6 L551.6,239.2 L545.4,234.1 L550.2,239.9 L544.7,235.5" fill="none" stroke="#2a78d6" stroke-width="1.6" stroke-linejoin="round" />
<path d="M668.0,181.0 L627.5,140.5 L605.2,126.7 L605.0,136.8 L614.3,155.7 L624.4,175.8 L628.4,190.9 L623.9,197.8 L612.8,197.8 L598.2,193.9 L583.3,189.9 L571.5,188.9 L564.7,192.6 L562.7,200.8 L564.1,211.8 L566.5,223.5 L567.5,233.6 L565.7,240.3 L560.5,243.0 L552.7,242.7 L543.9,240.8 L535.9,239.1 L530.3,239.3 L527.8,241.9 L528.1,246.8 L530.2,252.9 L532.5,258.7 L533.7,262.7 L532.9,264.3 L530.2,263.4 L526.4,261.0 L522.7,258.1 L520.3,256.1 L519.8,255.7 L521.3,256.8 L524.2,258.9 L527.3,261.1 L529.7,262.1 L530.8,261.6 L530.4,259.6 L529.3,256.6 L528.3,253.5 L528.1,251.2 L529.2,250.2 L531.5,250.3 L534.3,251.0 L536.9,251.4 L538.6,251.1 L539.3,249.7 L539.0,247.6 L538.5,245.3 L538.3,243.4 L538.8,242.5 L540.1,242.5 L541.8,243.0 L543.4,243.6 L544.3,243.7 L544.4,243.1 L543.9,242.0 L543.1,240.9 L542.6,240.2 L542.6,240.1 L543.1,240.7 L543.8,241.5 L544.3,242.3 L544.3,242.7 L543.7,242.6 L542.9,242.2 L542.0,241.9 L541.5,241.9 L541.4,242.4 L541.5,243.2 L541.7,244.0 L541.7,244.6 L541.3,244.8 L540.6,244.7 L540.0,244.5 L539.5,244.5 L539.3,244.8 L539.4,245.3 L539.6,245.8 L539.7,246.1 L539.5,246.2 L539.2,246.1 L538.8,245.9 L538.6,245.7 L538.6,245.8 L538.9,245.9 L539.1,246.2 L539.3,246.2 L539.3,246.1 L539.2,245.9 L539.1,245.6 L539.1,245.4 L539.2,245.4 L539.5,245.4 L539.7,245.5 L539.9,245.5 L539.9,245.3 L539.9,245.1 L539.9,244.9 L539.9,244.8 L540.0,244.8 L540.2,244.9 L540.3,244.9 L540.4,244.9 L540.3,244.8 L540.3,244.6 L540.2,244.6 L540.2,244.6 L540.3,244.7 L540.4,244.7 L540.4,244.8 L540.3,244.8 L540.2,244.7 L540.2,244.7 L540.1,244.7 L540.1,244.8 L540.2,244.9 L540.2,245.0" fill="none" stroke="#eb6834" stroke-width="1.6" stroke-linejoin="round" />
<path d="M668.0,181.0 L540.0,245.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round" />
<circle cx="668.0" cy="181.0" r="3.2" fill="#0b0b0b" />
<circle cx="540.0" cy="245.0" r="3.2" fill="#0b0b0b" />
<path d="M668.0,181.0 L599.9,125.3 L652.0,189.0 L592.4,140.2 L638.0,196.0 L585.9,153.3 L625.8,202.1 L580.1,164.7 L615.1,207.5 L575.1,174.8 L605.7,212.1 L570.7,183.5 L597.5,216.3 L566.9,191.2 L590.3,219.8 L563.5,197.9 L584.0,223.0 L560.6,203.8 L578.5,225.7 L558.0,209.0 L573.7,228.1 L555.8,213.5 L569.5,230.2 L553.8,217.4 L565.8,232.1 L552.1,220.8 L562.6,233.7 L550.6,223.9 L559.8,235.1 L549.3,226.5 L557.3,236.3 L548.1,228.8 L555.1,237.4 L547.1,230.8 L553.3,238.4 L546.2,232.6 L551.6,239.2 L545.4,234.1 L550.2,239.9 L544.7,235.5" fill="none" stroke="#1baf7a" stroke-width="1.6" stroke-linejoin="round" stroke-dasharray="5 4" />
</g>
<circle cx="668.0" cy="181.0" r="4" fill="none" stroke="#0b0b0b" stroke-width="1.5" />
<text x="540.0" y="249.0" font-size="14" fill="#0b0b0b" text-anchor="middle">×</text>
<rect x="380" y="386" width="14" height="3" fill="#2a78d6" />
<text x="400" y="392" font-size="12" fill="#0b0b0b">gradient descent: 208 steps</text>
<rect x="380" y="403" width="14" height="3" fill="#eb6834" />
<text x="400" y="409" font-size="12" fill="#0b0b0b">Adam: 219 steps</text>
<rect x="380" y="420" width="14" height="3" fill="#0b0b0b" />
<text x="400" y="426" font-size="12" fill="#0b0b0b">Newton: 1 step</text>
<rect x="380" y="437" width="14" height="3" fill="#1baf7a" />
<text x="400" y="443" font-size="12" fill="#0b0b0b">diagonal scaling by curvature: 208 steps (on top of gradient descent)</text>
</svg>

<h2 id="idea-one-a-separate-step-size-for-each-direction">Idea one: a separate step size for each direction</h2>

<p>The obvious fix: the cross direction is steep, so take small steps there; the along direction is flat, so take
large steps there. Concretely, keep one account for east-west and one for north-south, and scale each step by how
strongly the ground curves in that direction.</p>

<p>In panel (a) the valley happens to lie along the axes, and the dashed green line does exactly this. East-west is
exactly the flat direction and north-south exactly the steep one, so separate scaling is exactly right: one step to
the bottom, on top of Newton’s line.</p>

<p>Now tilt the same valley by 45°, panel (b). Its shape has not changed at all; it is only rotated. Walking due east,
you now meet both the steep and the flat direction; walking due north, the same. The two accounts come out
identical, and scaling both by the same amount is no scaling at all. The dashed green line lies on top of gradient
descent’s blue line: 208 steps again.</p>

<p><strong>A separate step size per axis helps only when the valley lies along the axes.</strong></p>

<h2 id="idea-two-learn-the-shape-of-the-valley">Idea two: learn the shape of the valley</h2>

<p>Newton’s method keeps no per-axis accounts. It learns the shape of the whole valley: which way the long axis
points, which way the short axis points, and how strongly each one curves. Once you know the shape, you can compute
where the bottom is and step straight there.</p>

<p>Rotating the valley changes nothing, because Newton looks at the valley itself, not at the axes. In both panels the
black line reaches the bottom in one step.</p>

<h2 id="where-is-adam">Where is Adam?</h2>

<p>Adam is often described as idea one, implemented for deep learning: each parameter gets its own step size. But it
keeps a different account. Idea one records how the slope <em>curves</em> (curvature); Adam records how <em>steep</em> the slope is
(the size of the gradient).</p>

<p>Those are quite different. A spot can be steep because the wall there really is steep, or simply because you are
still far from the bottom. Adam cannot tell the two apart. And as you approach the bottom the slope flattens, so its
account keeps changing.</p>

<p>Measured: the orange Adam line, with the best of 13 learning rates, takes 213 steps in the aligned valley and 219 in
the tilted one, both about the same as gradient descent’s 208. In the aligned valley it does not get idea one’s
single step.</p>

<p>This does not mean Adam is bad. Two variables and no noise is not the setting Adam was designed for; it was built
for training with billions of parameters and noisy gradients. The example shows one thing only: <strong>Adam’s “separate
step size per parameter” is not scaling by curvature, let alone Newton.</strong></p>

<h2 id="so-why-doesnt-deep-learning-use-newton">So why doesn’t deep learning use Newton?</h2>

<p>Because learning the shape of the valley is too expensive. The shape is a matrix (the Hessian): n parameters give
n × n numbers. A large model has billions of parameters, so the matrix cannot even be stored, let alone inverted.
And each step sees only a small batch of data, so the feel of the ground underfoot is itself noisy, and so would be
any shape computed from it.</p>

<p>Self-consistency problems in physics are the opposite: a few to a few hundred variables, so the shape can be
computed; no noise; and the answer must be very precise (residuals of 10⁻¹⁰). Paying to learn the shape is worth it
there, which is why Newton is used.</p>

<p>There are compromises in between. In deep learning, K-FAC and Shampoo approximate the matrix layer by layer with
Kronecker products, which keeps some of the coupling between directions that a diagonal throws away. In physics,
Broyden and Anderson mixing build the matrix up piece by piece from past steps.</p>

<h2 id="the-math">The math</h2>

<h3 id="one-framework">One framework</h3>

<p>All the methods below can be written as</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x ← x − P ∇L(x)
</code></pre></div></div>

<p>and differ only in the preconditioner P:</p>

<table>
  <thead>
    <tr>
      <th>method</th>
      <th>P</th>
      <th>based on</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>gradient descent</td>
      <td>η I</td>
      <td>nothing: one step size for all directions</td>
    </tr>
    <tr>
      <td>diagonal scaling by curvature</td>
      <td>η diag(H)⁻¹</td>
      <td>the curvature along each coordinate</td>
    </tr>
    <tr>
      <td>Adam</td>
      <td>η diag(1/(√v̂ + ε)), v̂ a running average of squared gradients</td>
      <td>the size of the gradient along each coordinate</td>
    </tr>
    <tr>
      <td>Newton</td>
      <td>H⁻¹</td>
      <td>the full curvature matrix</td>
    </tr>
  </tbody>
</table>

<h3 id="the-example">The example</h3>

<p>The loss is a quadratic L(x) = ½ xᵀ H x with its minimum at x = 0, and</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>H = Q(θ) diag(0.05, 1.5) Q(θ)ᵀ,     condition number κ = 1.5 / 0.05 = 30
</code></pre></div></div>

<p>where Q(θ) is a rotation by θ. Panel (a) is θ = 0, panel (b) θ = 45°. Both start at x₀ = (1, 0.5), and
“converged” means |x| has fallen to 10⁻⁶ |x₀|.</p>

<p><strong>Gradient descent.</strong> Along eigendirection i the error is multiplied by 1 − η λᵢ each step. The best choice makes
the two factors equal in size: η = 2/(λ_min + λ_max) = 2/1.55 ≈ 1.29, with factor (κ − 1)/(κ + 1) = 29/31 ≈ 0.935,
so about ln 10⁻⁶ / ln 0.935 ≈ 208 steps. This depends only on the eigenvalues of H, not on θ, so both orientations
take 208 steps.</p>

<p><strong>Diagonal scaling.</strong> At θ = 0, diag(H) = H, so P = H⁻¹: this is Newton, one step. At θ = 45°,</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>H = [[ 0.775, −0.725],
     [−0.725,  0.775]]
</code></pre></div></div>

<p>Both diagonal entries are 0.775, so diag(H)⁻¹ is the identity times a constant: gradient descent with a different
learning rate, still 208 steps. The off-diagonal −0.725 is what carries the tilt, and diagonal scaling throws it
away.</p>

<p><strong>Adam.</strong> Each step</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>m ← β₁ m + (1 − β₁) g          (running average of the gradient)
v ← β₂ v + (1 − β₂) g²         (running average of the squared gradient, per coordinate)
x ← x − η m̂ / (√v̂ + ε)          (m̂, v̂: bias-corrected)
</code></pre></div></div>

<p>The denominator √v̂ measures the size of the gradient. In a quadratic, the gradient along coordinate i is roughly
the curvature times the distance from the minimum along that direction, so Adam’s scaling mixes curvature with
distance, and the distance keeps changing. (This is an explanation of the result, not something measured
separately.) β₁ = 0.9, β₂ = 0.999, ε = 10⁻⁸; only the learning rate was tuned, over 13 log-spaced values from 10⁻³
to 1. The best was 0.1 in the aligned valley (213 steps) and 0.316 in the tilted one (219 steps).</p>

<p><strong>Gradient descent with momentum (heavy ball).</strong> Adam’s m is a form of momentum, so here is momentum on its own:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x_{k+1} = x_k − α ∇L(x_k) + β (x_k − x_{k−1})
</code></pre></div></div>

<p>With Polyak’s optimal parameters α = 4/(√λ_max + √λ_min)² and β = ((√κ − 1)/(√κ + 1))², the asymptotic factor
improves from (κ − 1)/(κ + 1) to (√κ − 1)/(√κ + 1) ≈ 0.691. Measured: 48 steps aligned, 47 tilted (not drawn in the
figure). Momentum in effect takes the square root of the condition number, 208 steps down to about 48. But like
gradient descent it uses one step size for all directions, so the orientation of the valley makes no difference.</p>

<p><strong>Newton.</strong> P = H⁻¹ reaches the minimum of a quadratic in one step, whatever θ is.</p>

<h3 id="measured-step-counts">Measured step counts</h3>

<table>
  <thead>
    <tr>
      <th>method</th>
      <th>aligned (θ = 0)</th>
      <th>tilted (θ = 45°)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>gradient descent, η = 1.29</td>
      <td>208</td>
      <td>208</td>
    </tr>
    <tr>
      <td>diagonal scaling by curvature</td>
      <td>1</td>
      <td>208</td>
    </tr>
    <tr>
      <td>heavy ball (Polyak parameters)</td>
      <td>48</td>
      <td>47</td>
    </tr>
    <tr>
      <td>Adam (best learning rate)</td>
      <td>213</td>
      <td>219</td>
    </tr>
    <tr>
      <td>Newton</td>
      <td>1</td>
      <td>1</td>
    </tr>
  </tbody>
</table>

<p>Gradient descent, heavy ball and Newton do not depend on the axes; their step counts depend only on the shape of the
valley, so rotating it changes nothing. Diagonal scaling and Adam do depend on the axes, so rotation changes their
counts. The difference: diagonal scaling goes from 1 step to 208, while Adam never had the advantage in the first
place and takes about 210 steps either way.</p>

<h3 id="limits">Limits</h3>

<ul>
  <li>Two variables, no noise, an exact quadratic, one starting point.</li>
  <li>For Adam only the learning rate was tuned; β₁ and β₂ are the defaults.</li>
  <li>This shows what each preconditioner is based on. It says nothing about how well Adam trains neural networks.</li>
</ul>

<h2 id="in-one-sentence">In one sentence</h2>

<p>Adam and Newton both “rescale the step per direction”. But Adam looks at how steep the slope is, one axis at a
time; Newton looks at the full shape of the valley. When the valley is tilted, only the method that sees the shape
helps.</p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="optimization" /><summary type="html"><![CDATA[Adam and Newton's method both want small steps along steep directions and large steps along flat ones. Put the same valley along the axes and then tilt it: diagonal scaling by curvature goes from 1 step to 208, Newton takes 1 step in both, and Adam takes about as long as gradient descent in both. Pictures first, then the math.]]></summary></entry><entry xml:lang="zh-CN"><title type="html">线性混合为什么会卡住，Newton 法改进了什么：以 Gutzwiller 自洽为例</title><link href="https://thomaszhang2661.github.io/2026/11/23/newton-gutzwiller-self-consistency-zh/" rel="alternate" type="text/html" title="线性混合为什么会卡住，Newton 法改进了什么：以 Gutzwiller 自洽为例" /><published>2026-11-23T00:00:00+00:00</published><updated>2026-11-23T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/23/newton-gutzwiller-self-consistency-zh</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/23/newton-gutzwiller-self-consistency-zh/"><![CDATA[<p><em>标签：计算物理。这是我早年物理工作的一篇旁支笔记；这个博客大部分写的是大模型推理。</em></p>

<p><a href="/2026/11/23/newton-gutzwiller-self-consistency/">English version</a></p>

<p>电子结构里大多数自洽方法（DFT、DFT+U、DFT+DMFT、Gutzwiller 近似）最后都是同一个循环：猜一组参数，用它算出一组新的，再代回去，直到输入等于输出。最常用的跑法是线性混合，最常见的抱怨也是它：在物理最有意思的地方收敛很慢，或者干脆不收敛。</p>

<p>2017 年，我和合作者在一个 LDA+Gutzwiller 程序里用 Newton 法替换了线性混合（<a href="https://doi.org/10.1088/1674-1056/26/1/017103">Chinese Physics B 26, 017103</a>）。这篇文章不讲材料细节，只讲思路：线性混合为什么会变慢，为什么会漏掉解，Newton 法改了什么，Jacobian 要花多少代价。</p>

<h2 id="这个循环">这个循环</h2>

<p>在 Gutzwiller 近似里，每个关联轨道 α 有两个参数：准粒子权重 R_α（在 0 到 1 之间，把能带压窄）和在位能修正 E_α（把能带整体移动）。循环分两半：</p>

<ol>
  <li><strong>单粒子部分。</strong> 给定 R 和 E，构造重整化后的能带哈密顿量，在每个 k 点对角化，得到轨道占据 n_α 和动能导数 ∂E_kin/∂R_α。</li>
  <li><strong>多体部分。</strong> 给定 n 和 ∂E_kin/∂R，在局域 Fock 态上构造一个小哈密顿量，求基态，得到新的 R 和 E。这一半里面还有一个内循环：要调局域拉格朗日乘子 λ_α，使局域占据等于 n_α。</li>
</ol>

<p>两半合起来就是一个映射 x_out = g(x_in)，x 包括所有 R_α 和 E_α。解就是不动点 g(x) = x。</p>

<p>下面的内容不依赖 Gutzwiller 的细节，任何自洽循环都可以这样看。</p>

<h2 id="线性混合的速度上限">线性混合的速度上限</h2>

<p>线性混合只把输出的一部分反馈回去：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x_{k+1} = x_k + λ (g(x_k) − x_k),      0 &lt; λ ≤ 1
</code></pre></div></div>

<p>在不动点 x* 附近，记误差 e_k = x_k − x<em>，J = ∂g/∂x（在 x</em> 处取值）。一阶近似下：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>e_{k+1} = (I + λ (J − I)) e_k
</code></pre></div></div>

<p>所以 J 的每个本征方向各走各的。如果 J 在某个方向上的本征值是 μ，这个方向的误差每步乘以 <strong>1 − λ(1 − μ)</strong>。线性混合的所有性质都可以从这个因子推出来。</p>

<p><strong>一个小例子。</strong> 假设 J 有两个本征值，μ_A = 0.95，μ_B = −0.5。</p>

<ul>
  <li>λ = 1（直接迭代）：A 方向每步缩小到 0.95 倍，误差降到 10⁻⁶ 需要 ln(10⁻⁶)/ln(0.95) ≈ 270 步。</li>
  <li>只看 A 方向，λ 越大越好：λ = 1/(1 − 0.95) = 20 一步就能消掉它。</li>
  <li>但 B 方向的因子是 1 − 1.5λ，必须大于 −1，所以 λ &lt; 4/3。λ = 20 时 B 方向每步乘以 −29，来回振荡并且发散。</li>
  <li>最好的折中是让两个因子绝对值相等：λ = 2/1.55 ≈ 1.29，两个方向每步都缩小到 0.935 倍，仍然需要 ≈ 208 步。</li>
</ul>

<p>一个慢方向加一个振荡方向，就给出一个硬上限，怎么调 λ 都越不过去。只要 J 的本征值分布得很开，线性混合就会慢。</p>

<p>非线性的 toy 里也是一样：取一个两变量映射 g，它在不动点处的 Jacobian 恰好是这两个本征值，两个本征方向转了个角度互相混合，再加一个二次项。初值相同，误差降到 10⁻⁶ 倍，线性混合在 λ = 1 时要 273 步，λ = 1.29 时要 228 步，λ = 1.4 时发散；Newton 法只要 4 步（图 a）。</p>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 740 440" width="100%" role="img" aria-label="两变量 toy 不动点上每一步的误差" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Noto Sans SC', Helvetica, Arial, sans-serif;max-width:740px">
<rect width="740" height="440" fill="#fcfcfb" />
<text x="20" y="32" font-size="19" font-weight="600" fill="#0b0b0b">两变量 toy 不动点上每一步的误差</text>
<text x="20" y="54" font-size="12.5" fill="#52514e">g(x) 在不动点处的 Jacobian 本征值见各图标题；初值相同；纵轴 |x − x*| 取对数</text>
<text x="70" y="96" font-size="13.5" font-weight="600" fill="#0b0b0b">(a) μ = 0.95 和 −0.5</text>
<line x1="70" x2="370" y1="380.0" y2="380.0" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="384.0" font-size="11.5" fill="#7a7974" text-anchor="end">1e-14</text>
<line x1="70" x2="370" y1="316.5" y2="316.5" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="320.5" font-size="11.5" fill="#7a7974" text-anchor="end">1e-10</text>
<line x1="70" x2="370" y1="252.9" y2="252.9" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="256.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e-6</text>
<line x1="70" x2="370" y1="189.4" y2="189.4" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="193.4" font-size="11.5" fill="#7a7974" text-anchor="end">1e-2</text>
<line x1="70" x2="370" y1="125.9" y2="125.9" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="129.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e2</text>
<text x="70.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">0</text>
<text x="170.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">100</text>
<text x="270.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">200</text>
<text x="370.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">300</text>
<line x1="70" x2="370" y1="380" y2="380" stroke="#7a7974" stroke-width="1" />
<text x="220.0" y="416" font-size="12" fill="#52514e" text-anchor="middle">步数</text>
<path d="M70.0,168.0 L71.0,171.5 L72.0,172.6 L73.0,173.3 L74.0,173.5 L75.0,173.8 L76.0,173.9 L77.0,174.1 L78.0,174.3 L79.0,174.5 L80.0,174.7 L81.0,174.9 L82.0,175.1 L83.0,175.3 L84.0,175.5 L85.0,175.7 L86.0,175.9 L87.0,176.1 L88.0,176.3 L89.0,176.6 L90.0,176.8 L91.0,177.0 L92.0,177.3 L93.0,177.5 L94.0,177.7 L95.0,178.0 L96.0,178.2 L97.0,178.5 L98.0,178.8 L99.0,179.0 L100.0,179.3 L101.0,179.6 L102.0,179.8 L103.0,180.1 L104.0,180.4 L105.0,180.7 L106.0,181.0 L107.0,181.3 L108.0,181.6 L109.0,181.8 L110.0,182.1 L111.0,182.4 L112.0,182.7 L113.0,183.0 L114.0,183.4 L115.0,183.7 L116.0,184.0 L117.0,184.3 L118.0,184.6 L119.0,184.9 L120.0,185.2 L121.0,185.5 L122.0,185.9 L123.0,186.2 L124.0,186.5 L125.0,186.8 L126.0,187.2 L127.0,187.5 L128.0,187.8 L129.0,188.1 L130.0,188.5 L131.0,188.8 L132.0,189.1 L133.0,189.5 L134.0,189.8 L135.0,190.2 L136.0,190.5 L137.0,190.8 L138.0,191.2 L139.0,191.5 L140.0,191.8 L141.0,192.2 L142.0,192.5 L143.0,192.9 L144.0,193.2 L145.0,193.5 L146.0,193.9 L147.0,194.2 L148.0,194.6 L149.0,194.9 L150.0,195.3 L151.0,195.6 L152.0,196.0 L153.0,196.3 L154.0,196.7 L155.0,197.0 L156.0,197.3 L157.0,197.7 L158.0,198.0 L159.0,198.4 L160.0,198.7 L161.0,199.1 L162.0,199.4 L163.0,199.8 L164.0,200.1 L165.0,200.5 L166.0,200.8 L167.0,201.2 L168.0,201.5 L169.0,201.9 L170.0,202.2 L171.0,202.6 L172.0,202.9 L173.0,203.3 L174.0,203.6 L175.0,204.0 L176.0,204.3 L177.0,204.7 L178.0,205.1 L179.0,205.4 L180.0,205.8 L181.0,206.1 L182.0,206.5 L183.0,206.8 L184.0,207.2 L185.0,207.5 L186.0,207.9 L187.0,208.2 L188.0,208.6 L189.0,208.9 L190.0,209.3 L191.0,209.6 L192.0,210.0 L193.0,210.3 L194.0,210.7 L195.0,211.0 L196.0,211.4 L197.0,211.7 L198.0,212.1 L199.0,212.5 L200.0,212.8 L201.0,213.2 L202.0,213.5 L203.0,213.9 L204.0,214.2 L205.0,214.6 L206.0,214.9 L207.0,215.3 L208.0,215.6 L209.0,216.0 L210.0,216.3 L211.0,216.7 L212.0,217.0 L213.0,217.4 L214.0,217.8 L215.0,218.1 L216.0,218.5 L217.0,218.8 L218.0,219.2 L219.0,219.5 L220.0,219.9 L221.0,220.2 L222.0,220.6 L223.0,220.9 L224.0,221.3 L225.0,221.6 L226.0,222.0 L227.0,222.4 L228.0,222.7 L229.0,223.1 L230.0,223.4 L231.0,223.8 L232.0,224.1 L233.0,224.5 L234.0,224.8 L235.0,225.2 L236.0,225.5 L237.0,225.9 L238.0,226.2 L239.0,226.6 L240.0,226.9 L241.0,227.3 L242.0,227.7 L243.0,228.0 L244.0,228.4 L245.0,228.7 L246.0,229.1 L247.0,229.4 L248.0,229.8 L249.0,230.1 L250.0,230.5 L251.0,230.8 L252.0,231.2 L253.0,231.5 L254.0,231.9 L255.0,232.3 L256.0,232.6 L257.0,233.0 L258.0,233.3 L259.0,233.7 L260.0,234.0 L261.0,234.4 L262.0,234.7 L263.0,235.1 L264.0,235.4 L265.0,235.8 L266.0,236.1 L267.0,236.5 L268.0,236.9 L269.0,237.2 L270.0,237.6 L271.0,237.9 L272.0,238.3 L273.0,238.6 L274.0,239.0 L275.0,239.3 L276.0,239.7 L277.0,240.0 L278.0,240.4 L279.0,240.7 L280.0,241.1 L281.0,241.5 L282.0,241.8 L283.0,242.2 L284.0,242.5 L285.0,242.9 L286.0,243.2 L287.0,243.6 L288.0,243.9 L289.0,244.3 L290.0,244.6 L291.0,245.0 L292.0,245.3 L293.0,245.7 L294.0,246.1 L295.0,246.4 L296.0,246.8 L297.0,247.1 L298.0,247.5 L299.0,247.8 L300.0,248.2 L301.0,248.5 L302.0,248.9 L303.0,249.2 L304.0,249.6 L305.0,249.9 L306.0,250.3 L307.0,250.7 L308.0,251.0 L309.0,251.4 L310.0,251.7 L311.0,252.1 L312.0,252.4 L313.0,252.8 L314.0,253.1 L315.0,253.5 L316.0,253.8 L317.0,254.2 L318.0,254.5 L319.0,254.9 L320.0,255.3 L321.0,255.6 L322.0,256.0 L323.0,256.3 L324.0,256.7 L325.0,257.0 L326.0,257.4 L327.0,257.7 L328.0,258.1 L329.0,258.4 L330.0,258.8 L331.0,259.1 L332.0,259.5 L333.0,259.9 L334.0,260.2 L335.0,260.6 L336.0,260.9 L337.0,261.3 L338.0,261.6 L339.0,262.0 L340.0,262.3 L341.0,262.7 L342.0,263.0 L343.0,263.4 L344.0,263.7 L345.0,264.1 L346.0,264.5 L347.0,264.8 L348.0,265.2 L349.0,265.5 L350.0,265.9 L351.0,266.2 L352.0,266.6 L353.0,266.9 L354.0,267.3 L355.0,267.6 L356.0,268.0 L357.0,268.3 L358.0,268.7 L359.0,269.0 L360.0,269.4 L361.0,269.8 L362.0,270.1 L363.0,270.5 L364.0,270.8 L365.0,271.2 L366.0,271.5 L367.0,271.9 L368.0,272.2 L369.0,272.6 L370.0,272.9" fill="none" stroke="#2a78d6" stroke-width="1.8" stroke-linejoin="round"><title>线性混合 λ = 1</title></path>
<path d="M70.0,168.0 L71.0,168.9 L72.0,168.5 L73.0,169.4 L74.0,169.0 L75.0,169.8 L76.0,169.5 L77.0,170.2 L78.0,170.0 L79.0,170.6 L80.0,170.4 L81.0,171.0 L82.0,170.9 L83.0,171.4 L84.0,171.3 L85.0,171.8 L86.0,171.8 L87.0,172.3 L88.0,172.2 L89.0,172.7 L90.0,172.7 L91.0,173.2 L92.0,173.2 L93.0,173.7 L94.0,173.7 L95.0,174.2 L96.0,174.3 L97.0,174.7 L98.0,174.9 L99.0,175.3 L100.0,175.4 L101.0,175.9 L102.0,176.1 L103.0,176.5 L104.0,176.7 L105.0,177.1 L106.0,177.4 L107.0,177.8 L108.0,178.0 L109.0,178.5 L110.0,178.7 L111.0,179.2 L112.0,179.5 L113.0,179.9 L114.0,180.2 L115.0,180.6 L116.0,181.0 L117.0,181.4 L118.0,181.7 L119.0,182.2 L120.0,182.5 L121.0,183.0 L122.0,183.3 L123.0,183.8 L124.0,184.2 L125.0,184.6 L126.0,185.0 L127.0,185.4 L128.0,185.8 L129.0,186.3 L130.0,186.7 L131.0,187.1 L132.0,187.5 L133.0,188.0 L134.0,188.4 L135.0,188.8 L136.0,189.3 L137.0,189.7 L138.0,190.1 L139.0,190.6 L140.0,191.0 L141.0,191.5 L142.0,191.9 L143.0,192.4 L144.0,192.8 L145.0,193.2 L146.0,193.7 L147.0,194.1 L148.0,194.6 L149.0,195.0 L150.0,195.5 L151.0,195.9 L152.0,196.4 L153.0,196.8 L154.0,197.3 L155.0,197.7 L156.0,198.2 L157.0,198.6 L158.0,199.1 L159.0,199.6 L160.0,200.0 L161.0,200.5 L162.0,200.9 L163.0,201.4 L164.0,201.8 L165.0,202.3 L166.0,202.7 L167.0,203.2 L168.0,203.7 L169.0,204.1 L170.0,204.6 L171.0,205.0 L172.0,205.5 L173.0,205.9 L174.0,206.4 L175.0,206.9 L176.0,207.3 L177.0,207.8 L178.0,208.2 L179.0,208.7 L180.0,209.2 L181.0,209.6 L182.0,210.1 L183.0,210.5 L184.0,211.0 L185.0,211.4 L186.0,211.9 L187.0,212.4 L188.0,212.8 L189.0,213.3 L190.0,213.7 L191.0,214.2 L192.0,214.7 L193.0,215.1 L194.0,215.6 L195.0,216.0 L196.0,216.5 L197.0,217.0 L198.0,217.4 L199.0,217.9 L200.0,218.3 L201.0,218.8 L202.0,219.3 L203.0,219.7 L204.0,220.2 L205.0,220.6 L206.0,221.1 L207.0,221.6 L208.0,222.0 L209.0,222.5 L210.0,222.9 L211.0,223.4 L212.0,223.9 L213.0,224.3 L214.0,224.8 L215.0,225.2 L216.0,225.7 L217.0,226.2 L218.0,226.6 L219.0,227.1 L220.0,227.5 L221.0,228.0 L222.0,228.5 L223.0,228.9 L224.0,229.4 L225.0,229.8 L226.0,230.3 L227.0,230.8 L228.0,231.2 L229.0,231.7 L230.0,232.1 L231.0,232.6 L232.0,233.1 L233.0,233.5 L234.0,234.0 L235.0,234.4 L236.0,234.9 L237.0,235.4 L238.0,235.8 L239.0,236.3 L240.0,236.7 L241.0,237.2 L242.0,237.7 L243.0,238.1 L244.0,238.6 L245.0,239.0 L246.0,239.5 L247.0,240.0 L248.0,240.4 L249.0,240.9 L250.0,241.3 L251.0,241.8 L252.0,242.3 L253.0,242.7 L254.0,243.2 L255.0,243.6 L256.0,244.1 L257.0,244.6 L258.0,245.0 L259.0,245.5 L260.0,245.9 L261.0,246.4 L262.0,246.9 L263.0,247.3 L264.0,247.8 L265.0,248.2 L266.0,248.7 L267.0,249.2 L268.0,249.6 L269.0,250.1 L270.0,250.5 L271.0,251.0 L272.0,251.5 L273.0,251.9 L274.0,252.4 L275.0,252.8 L276.0,253.3 L277.0,253.8 L278.0,254.2 L279.0,254.7 L280.0,255.1 L281.0,255.6 L282.0,256.1 L283.0,256.5 L284.0,257.0 L285.0,257.4 L286.0,257.9 L287.0,258.4 L288.0,258.8 L289.0,259.3 L290.0,259.7 L291.0,260.2 L292.0,260.7 L293.0,261.1 L294.0,261.6 L295.0,262.0 L296.0,262.5 L297.0,263.0 L298.0,263.4 L299.0,263.9 L300.0,264.3 L301.0,264.8 L302.0,265.3 L303.0,265.7 L304.0,266.2 L305.0,266.6 L306.0,267.1 L307.0,267.6 L308.0,268.0 L309.0,268.5 L310.0,268.9 L311.0,269.4 L312.0,269.9 L313.0,270.3 L314.0,270.8 L315.0,271.2 L316.0,271.7 L317.0,272.2 L318.0,272.6 L319.0,273.1 L320.0,273.5 L321.0,274.0 L322.0,274.5 L323.0,274.9 L324.0,275.4 L325.0,275.8 L326.0,276.3 L327.0,276.8 L328.0,277.2 L329.0,277.7 L330.0,278.1 L331.0,278.6 L332.0,279.1 L333.0,279.5 L334.0,280.0 L335.0,280.4 L336.0,280.9 L337.0,281.4 L338.0,281.8 L339.0,282.3 L340.0,282.7 L341.0,283.2 L342.0,283.7 L343.0,284.1 L344.0,284.6 L345.0,285.0 L346.0,285.5 L347.0,286.0 L348.0,286.4 L349.0,286.9 L350.0,287.3 L351.0,287.8 L352.0,288.3 L353.0,288.7 L354.0,289.2 L355.0,289.6 L356.0,290.1 L357.0,290.6 L358.0,291.0 L359.0,291.5 L360.0,291.9 L361.0,292.4 L362.0,292.9 L363.0,293.3 L364.0,293.8 L365.0,294.2 L366.0,294.7 L367.0,295.2 L368.0,295.6 L369.0,296.1 L370.0,296.5" fill="none" stroke="#1baf7a" stroke-width="1.8" stroke-linejoin="round"><title>线性混合 λ = 1.29</title></path>
<path d="M70.0,168.0 L71.0,168.0 L72.0,166.7 L73.0,166.8 L74.0,165.3 L75.0,165.6 L76.0,163.9 L77.0,164.4 L78.0,162.5 L79.0,163.2 L80.0,161.1 L81.0,162.0 L82.0,159.7 L83.0,160.8 L84.0,158.2 L85.0,159.5 L86.0,156.5 L87.0,158.0 L88.0,154.5 L89.0,155.9 L90.0,151.6 L91.0,152.5 L92.0,146.6 L93.0,145.3 L94.0,134.4 L95.0,122.9 L96.0,110.0" fill="none" stroke="#eb6834" stroke-width="1.8" stroke-linejoin="round"><title>线性混合 λ = 1.4</title></path>
<text x="100.0" y="122.0" font-size="11" fill="#eb6834">发散</text>
<path d="M70.0,168.0 L71.0,185.0 L72.0,195.4 L73.0,222.0 L74.0,274.7 L75.0,380.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round"><title>Newton</title></path>
<circle cx="70.0" cy="168.0" r="3" fill="#0b0b0b" />
<circle cx="71.0" cy="185.0" r="3" fill="#0b0b0b" />
<circle cx="72.0" cy="195.4" r="3" fill="#0b0b0b" />
<circle cx="73.0" cy="222.0" r="3" fill="#0b0b0b" />
<circle cx="74.0" cy="274.7" r="3" fill="#0b0b0b" />
<circle cx="75.0" cy="380.0" r="3" fill="#0b0b0b" />
<rect x="220" y="312" width="12" height="3" fill="#2a78d6" />
<text x="238" y="317" font-size="11.5" fill="#0b0b0b">线性混合 λ = 1</text>
<rect x="220" y="328" width="12" height="3" fill="#1baf7a" />
<text x="238" y="333" font-size="11.5" fill="#0b0b0b">线性混合 λ = 1.29</text>
<rect x="220" y="344" width="12" height="3" fill="#eb6834" />
<text x="238" y="349" font-size="11.5" fill="#0b0b0b">线性混合 λ = 1.4</text>
<rect x="220" y="360" width="12" height="3" fill="#0b0b0b" />
<text x="238" y="365" font-size="11.5" fill="#0b0b0b">Newton</text>
<text x="430" y="96" font-size="13.5" font-weight="600" fill="#0b0b0b">(b) μ = 1.2 和 0.5（排斥）</text>
<line x1="430" x2="720" y1="380.0" y2="380.0" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="384.0" font-size="11.5" fill="#7a7974" text-anchor="end">1e-14</text>
<line x1="430" x2="720" y1="316.5" y2="316.5" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="320.5" font-size="11.5" fill="#7a7974" text-anchor="end">1e-10</text>
<line x1="430" x2="720" y1="252.9" y2="252.9" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="256.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e-6</text>
<line x1="430" x2="720" y1="189.4" y2="189.4" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="193.4" font-size="11.5" fill="#7a7974" text-anchor="end">1e-2</text>
<line x1="430" x2="720" y1="125.9" y2="125.9" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="129.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e2</text>
<text x="430.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">0</text>
<text x="502.5" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">100</text>
<text x="575.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">200</text>
<text x="647.5" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">300</text>
<text x="720.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">400</text>
<line x1="430" x2="720" y1="380" y2="380" stroke="#7a7974" stroke-width="1" />
<text x="575.0" y="416" font-size="12" fill="#52514e" text-anchor="middle">步数</text>
<path d="M430.0,168.0 L430.7,169.9 L431.4,170.1 L432.2,169.1 L432.9,167.7 L433.6,166.2 L434.4,164.5 L435.1,162.6 L435.8,160.6 L436.5,158.4 L437.2,155.8 L438.0,152.5 L438.7,148.2 L439.4,142.0 L440.1,131.6 L440.9,112.2 L441.6,110.0" fill="none" stroke="#2a78d6" stroke-width="1.8" stroke-linejoin="round"><title>线性混合 λ = 1</title></path>
<text x="445.6" y="122.0" font-size="11" fill="#2a78d6">发散</text>
<path d="M430.0,168.0 L430.7,168.2 L431.4,168.4 L432.2,168.6 L432.9,168.7 L433.6,168.9 L434.4,169.1 L435.1,169.2 L435.8,169.3 L436.5,169.4 L437.2,169.5 L438.0,169.6 L438.7,169.7 L439.4,169.7 L440.1,169.8 L440.9,169.8 L441.6,169.8 L442.3,169.8 L443.1,169.7 L443.8,169.7 L444.5,169.7 L445.2,169.6 L445.9,169.5 L446.7,169.4 L447.4,169.4 L448.1,169.3 L448.9,169.2 L449.6,169.0 L450.3,168.9 L451.0,168.8 L451.8,168.7 L452.5,168.5 L453.2,168.4 L453.9,168.2 L454.6,168.1 L455.4,167.9 L456.1,167.8 L456.8,167.6 L457.6,167.5 L458.3,167.3 L459.0,167.1 L459.7,167.0 L460.4,166.8 L461.2,166.6 L461.9,166.4 L462.6,166.2 L463.4,166.1 L464.1,165.9 L464.8,165.7 L465.5,165.5 L466.2,165.3 L467.0,165.1 L467.7,164.9 L468.4,164.7 L469.1,164.5 L469.9,164.3 L470.6,164.1 L471.3,163.9 L472.1,163.7 L472.8,163.5 L473.5,163.3 L474.2,163.1 L474.9,162.8 L475.7,162.6 L476.4,162.4 L477.1,162.2 L477.9,161.9 L478.6,161.7 L479.3,161.4 L480.0,161.2 L480.8,160.9 L481.5,160.7 L482.2,160.4 L482.9,160.2 L483.6,159.9 L484.4,159.6 L485.1,159.3 L485.8,159.0 L486.6,158.7 L487.3,158.4 L488.0,158.1 L488.7,157.8 L489.4,157.5 L490.2,157.1 L490.9,156.8 L491.6,156.4 L492.4,156.1 L493.1,155.7 L493.8,155.3 L494.5,154.8 L495.2,154.4 L496.0,153.9 L496.7,153.4 L497.4,152.9 L498.1,152.4 L498.9,151.8 L499.6,151.2 L500.3,150.5 L501.1,149.8 L501.8,149.0 L502.5,148.1 L503.2,147.2 L503.9,146.1 L504.7,144.8 L505.4,143.4 L506.1,141.6 L506.9,139.4 L507.6,136.6 L508.3,132.8 L509.0,127.0 L509.8,117.4 L510.5,110.0" fill="none" stroke="#1baf7a" stroke-width="1.8" stroke-linejoin="round"><title>线性混合 λ = 0.1</title></path>
<text x="514.5" y="122.0" font-size="11" fill="#1baf7a">发散</text>
<path d="M430.0,168.0 L430.7,168.0 L431.4,168.0 L432.2,168.0 L432.9,168.1 L433.6,168.1 L434.4,168.1 L435.1,168.1 L435.8,168.1 L436.5,168.2 L437.2,168.2 L438.0,168.2 L438.7,168.2 L439.4,168.2 L440.1,168.3 L440.9,168.3 L441.6,168.3 L442.3,168.3 L443.1,168.3 L443.8,168.4 L444.5,168.4 L445.2,168.4 L445.9,168.4 L446.7,168.4 L447.4,168.4 L448.1,168.5 L448.9,168.5 L449.6,168.5 L450.3,168.5 L451.0,168.5 L451.8,168.6 L452.5,168.6 L453.2,168.6 L453.9,168.6 L454.6,168.6 L455.4,168.6 L456.1,168.7 L456.8,168.7 L457.6,168.7 L458.3,168.7 L459.0,168.7 L459.7,168.7 L460.4,168.8 L461.2,168.8 L461.9,168.8 L462.6,168.8 L463.4,168.8 L464.1,168.8 L464.8,168.9 L465.5,168.9 L466.2,168.9 L467.0,168.9 L467.7,168.9 L468.4,168.9 L469.1,168.9 L469.9,169.0 L470.6,169.0 L471.3,169.0 L472.1,169.0 L472.8,169.0 L473.5,169.0 L474.2,169.0 L474.9,169.1 L475.7,169.1 L476.4,169.1 L477.1,169.1 L477.9,169.1 L478.6,169.1 L479.3,169.1 L480.0,169.2 L480.8,169.2 L481.5,169.2 L482.2,169.2 L482.9,169.2 L483.6,169.2 L484.4,169.2 L485.1,169.2 L485.8,169.3 L486.6,169.3 L487.3,169.3 L488.0,169.3 L488.7,169.3 L489.4,169.3 L490.2,169.3 L490.9,169.3 L491.6,169.3 L492.4,169.4 L493.1,169.4 L493.8,169.4 L494.5,169.4 L495.2,169.4 L496.0,169.4 L496.7,169.4 L497.4,169.4 L498.1,169.4 L498.9,169.4 L499.6,169.5 L500.3,169.5 L501.1,169.5 L501.8,169.5 L502.5,169.5 L503.2,169.5 L503.9,169.5 L504.7,169.5 L505.4,169.5 L506.1,169.5 L506.9,169.5 L507.6,169.5 L508.3,169.6 L509.0,169.6 L509.8,169.6 L510.5,169.6 L511.2,169.6 L511.9,169.6 L512.6,169.6 L513.4,169.6 L514.1,169.6 L514.8,169.6 L515.5,169.6 L516.3,169.6 L517.0,169.6 L517.7,169.6 L518.5,169.6 L519.2,169.6 L519.9,169.7 L520.6,169.7 L521.4,169.7 L522.1,169.7 L522.8,169.7 L523.5,169.7 L524.2,169.7 L525.0,169.7 L525.7,169.7 L526.4,169.7 L527.1,169.7 L527.9,169.7 L528.6,169.7 L529.3,169.7 L530.0,169.7 L530.8,169.7 L531.5,169.7 L532.2,169.7 L533.0,169.7 L533.7,169.7 L534.4,169.7 L535.1,169.7 L535.9,169.7 L536.6,169.7 L537.3,169.7 L538.0,169.7 L538.8,169.7 L539.5,169.7 L540.2,169.7 L540.9,169.7 L541.6,169.7 L542.4,169.7 L543.1,169.7 L543.8,169.7 L544.5,169.7 L545.3,169.7 L546.0,169.7 L546.7,169.7 L547.5,169.7 L548.2,169.7 L548.9,169.7 L549.6,169.7 L550.4,169.7 L551.1,169.7 L551.8,169.7 L552.5,169.7 L553.2,169.7 L554.0,169.7 L554.7,169.7 L555.4,169.7 L556.1,169.7 L556.9,169.7 L557.6,169.7 L558.3,169.7 L559.0,169.7 L559.8,169.7 L560.5,169.7 L561.2,169.7 L562.0,169.7 L562.7,169.7 L563.4,169.7 L564.1,169.7 L564.9,169.7 L565.6,169.7 L566.3,169.7 L567.0,169.7 L567.8,169.7 L568.5,169.7 L569.2,169.7 L569.9,169.6 L570.6,169.6 L571.4,169.6 L572.1,169.6 L572.8,169.6 L573.5,169.6 L574.3,169.6 L575.0,169.6 L575.7,169.6 L576.5,169.6 L577.2,169.6 L577.9,169.6 L578.6,169.6 L579.4,169.6 L580.1,169.6 L580.8,169.6 L581.5,169.6 L582.2,169.6 L583.0,169.5 L583.7,169.5 L584.4,169.5 L585.1,169.5 L585.9,169.5 L586.6,169.5 L587.3,169.5 L588.0,169.5 L588.8,169.5 L589.5,169.5 L590.2,169.5 L591.0,169.5 L591.7,169.5 L592.4,169.5 L593.1,169.4 L593.9,169.4 L594.6,169.4 L595.3,169.4 L596.0,169.4 L596.8,169.4 L597.5,169.4 L598.2,169.4 L598.9,169.4 L599.6,169.4 L600.4,169.4 L601.1,169.3 L601.8,169.3 L602.5,169.3 L603.3,169.3 L604.0,169.3 L604.7,169.3 L605.5,169.3 L606.2,169.3 L606.9,169.3 L607.6,169.3 L608.4,169.3 L609.1,169.2 L609.8,169.2 L610.5,169.2 L611.2,169.2 L612.0,169.2 L612.7,169.2 L613.4,169.2 L614.1,169.2 L614.9,169.2 L615.6,169.1 L616.3,169.1 L617.0,169.1 L617.8,169.1 L618.5,169.1 L619.2,169.1 L620.0,169.1 L620.7,169.1 L621.4,169.1 L622.1,169.0 L622.9,169.0 L623.6,169.0 L624.3,169.0 L625.0,169.0 L625.8,169.0 L626.5,169.0 L627.2,169.0 L627.9,169.0 L628.6,168.9 L629.4,168.9 L630.1,168.9 L630.8,168.9 L631.5,168.9 L632.3,168.9 L633.0,168.9 L633.7,168.9 L634.5,168.8 L635.2,168.8 L635.9,168.8 L636.6,168.8 L637.4,168.8 L638.1,168.8 L638.8,168.8 L639.5,168.8 L640.2,168.7 L641.0,168.7 L641.7,168.7 L642.4,168.7 L643.1,168.7 L643.9,168.7 L644.6,168.7 L645.3,168.6 L646.0,168.6 L646.8,168.6 L647.5,168.6 L648.2,168.6 L649.0,168.6 L649.7,168.6 L650.4,168.6 L651.1,168.5 L651.9,168.5 L652.6,168.5 L653.3,168.5 L654.0,168.5 L654.8,168.5 L655.5,168.5 L656.2,168.4 L656.9,168.4 L657.6,168.4 L658.4,168.4 L659.1,168.4 L659.8,168.4 L660.5,168.4 L661.3,168.3 L662.0,168.3 L662.7,168.3 L663.5,168.3 L664.2,168.3 L664.9,168.3 L665.6,168.3 L666.4,168.2 L667.1,168.2 L667.8,168.2 L668.5,168.2 L669.2,168.2 L670.0,168.2 L670.7,168.1 L671.4,168.1 L672.1,168.1 L672.9,168.1 L673.6,168.1 L674.3,168.1 L675.0,168.1 L675.8,168.0 L676.5,168.0 L677.2,168.0 L678.0,168.0 L678.7,168.0 L679.4,168.0 L680.1,167.9 L680.9,167.9 L681.6,167.9 L682.3,167.9 L683.0,167.9 L683.8,167.9 L684.5,167.9 L685.2,167.8 L685.9,167.8 L686.6,167.8 L687.4,167.8 L688.1,167.8 L688.8,167.8 L689.5,167.7 L690.3,167.7 L691.0,167.7 L691.7,167.7 L692.5,167.7 L693.2,167.7 L693.9,167.6 L694.6,167.6 L695.4,167.6 L696.1,167.6 L696.8,167.6 L697.5,167.6 L698.2,167.6 L699.0,167.5 L699.7,167.5 L700.4,167.5 L701.2,167.5 L701.9,167.5 L702.6,167.5 L703.3,167.4 L704.0,167.4 L704.8,167.4 L705.5,167.4 L706.2,167.4 L707.0,167.4 L707.7,167.3 L708.4,167.3 L709.1,167.3 L709.8,167.3 L710.6,167.3 L711.3,167.3 L712.0,167.2 L712.8,167.2 L713.5,167.2 L714.2,167.2 L714.9,167.2 L715.6,167.2 L716.4,167.1 L717.1,167.1 L717.8,167.1 L718.5,167.1 L719.3,167.1 L720.0,167.0" fill="none" stroke="#eb6834" stroke-width="1.8" stroke-linejoin="round"><title>线性混合 λ = 0.01</title></path>
<path d="M430.0,168.0 L430.7,181.7 L431.4,208.9 L432.2,257.5 L432.9,351.8 L433.6,380.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round"><title>Newton</title></path>
<circle cx="430.0" cy="168.0" r="3" fill="#0b0b0b" />
<circle cx="430.7" cy="181.7" r="3" fill="#0b0b0b" />
<circle cx="431.4" cy="208.9" r="3" fill="#0b0b0b" />
<circle cx="432.2" cy="257.5" r="3" fill="#0b0b0b" />
<circle cx="432.9" cy="351.8" r="3" fill="#0b0b0b" />
<circle cx="433.6" cy="380.0" r="3" fill="#0b0b0b" />
<rect x="570" y="312" width="12" height="3" fill="#2a78d6" />
<text x="588" y="317" font-size="11.5" fill="#0b0b0b">线性混合 λ = 1</text>
<rect x="570" y="328" width="12" height="3" fill="#1baf7a" />
<text x="588" y="333" font-size="11.5" fill="#0b0b0b">线性混合 λ = 0.1</text>
<rect x="570" y="344" width="12" height="3" fill="#eb6834" />
<text x="588" y="349" font-size="11.5" fill="#0b0b0b">线性混合 λ = 0.01</text>
<rect x="570" y="360" width="12" height="3" fill="#0b0b0b" />
<text x="588" y="365" font-size="11.5" fill="#0b0b0b">Newton</text>
<text transform="translate(16,245.0) rotate(-90)" font-size="12" fill="#52514e" text-anchor="middle">|x − x*|</text>
</svg>

<p><strong>为什么相变附近会出现慢方向。</strong> 在相变点或分支点，解的性质发生变化，f(x) = g(x) − x 的根在这里退化：∂f/∂x = J − I 有零本征值，也就是 J 有一个等于 1 的本征值。越接近相变，μ 越接近 1，因子 1 − λ(1 − μ) 也越接近 1，与 λ 无关。这就是求解器里的临界慢化。2017 年论文里 SrVO₃ 的结果直接体现了这一点：远离 Brinkman–Rice 相变（U = 1 eV）时两种方法都只要几步；靠近相变（U = 6 eV）时，线性混合到第 60 步能量还在缓慢下降。</p>

<p><strong>线性混合找不到的解。</strong> 如果 J 有实本征值 μ &gt; 1，那么对任何 λ &gt; 0，因子 1 + λ(μ − 1) 都大于 1。这个不动点对线性混合是排斥的，混合系数取多小都没用，迭代永远落不到它上面。在 Brinkman–Rice 相变附近，Gutzwiller 方程有两个 R 不同的解（<a href="https://doi.org/10.1103/PhysRevB.85.035133">Lanatà et al. 2012</a>）。我们当时用线性混合始终找不到 R 小的那一支，用 Newton 法可以。排斥不动点是一个很可能的解释。图 (b) 是 toy 里的情形：本征值为 1.2 和 0.5 时，线性混合在 λ = 1、0.1、0.01 下都离解越来越远，Newton 法 4 步收敛。</p>

<h2 id="newton-法">Newton 法</h2>

<p>Newton 法不再把循环当作迭代，而是当作求根问题 f(x) = g(x) − x = 0：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x_{k+1} = x_k − (J_f)⁻¹ f(x_k),      J_f = ∂f/∂x = ∂g/∂x − I
</code></pre></div></div>

<p>（论文里把 Jacobian 写成 ∂x_out/∂x_in。因为 f = g − x，更新公式里用的矩阵是 ∂g/∂x − I。）</p>

<p>变化有两点。</p>

<ul>
  <li><strong>收敛速度不再取决于本征值分布得多开。</strong> 在非退化的根附近，Newton 法的误差大约每步平方一次：10⁻² → 10⁻⁴ → 10⁻⁸。慢方向和振荡方向一视同仁。上面两个方向的例子，如果映射是严格线性的，Newton 一步就到根。</li>
  <li><strong>不要求迭代在不动点处稳定。</strong> 只要 J_f 可逆，Newton 法就能收敛到这个根，包括线性混合永远到不了的 μ &gt; 1 的解。</li>
</ul>

<p>论文里，SrVO₃ 用 Newton 法外循环 4–6 步收敛。La₂O₃Fe₂Se₂ 的磁性原胞里有 16 个关联原子，内循环用线性混合一直找不到满足占据约束的 λ，Newton 法 6 步就找到了。</p>

<p>Newton 法有两个代价。</p>

<table>
  <tbody>
    <tr>
      <td><strong>需要好的初值。</strong> 离根较远时，一个完整的 Newton 步可能走过头。标准做法是线搜索：方向沿用 Newton 方向，只调步长。我们当时沿 Newton 方向做黄金分割搜索；更常见、更简单的是回溯（步长减半，直到残差</td>
      <td>f</td>
      <td>下降）。限制步长也有帮助，因为 R 和 E 的量级不同（R 在 1 左右，E 以 eV 计）。</td>
    </tr>
  </tbody>
</table>

<p><strong>需要 Jacobian。</strong> 这才是真正的代价。</p>

<h2 id="jacobian-从哪来">Jacobian 从哪来</h2>

<p>N 个关联自旋轨道对应 2N 个未知数（R_α 和 E_α）。La₂O₃Fe₂Se₂ 在不用对称性的情况下最多是 16 个原子 × 10 个自旋轨道 × 2 = 320 个。求 J_f 有三种办法。</p>

<ol>
  <li><strong>有限差分。</strong> 每个输入扰动一下，重跑一遍循环，做差。每个 Newton 步要 2N + 1 = 321 次完整循环，比整个线性混合过程还贵；精度还取决于差分步长取得好不好。</li>
  <li><strong>手推链式法则。</strong> 2017 年我们就是这样做的。沿循环的两半拆开 J：先由能带问题求 (n, ∂E_kin/∂R) 对 (R, E) 的导数，再由多体问题求 (R, E) 对 (n, ∂E_kin/∂R) 的导数，然后相乘。本征矢的导数用一阶微扰论。论文里光这些公式就占了三个附录。</li>
  <li><strong>自动微分。</strong> 用 JAX 或 PyTorch 把 g 写一遍，由框架精确算出 J。现在我会这么做。</li>
</ol>

<p>三种办法里最需要小心的都是内循环。乘子 λ 没有显式公式，是由”局域占据 N(λ; n, ∂E_kin/∂R) 等于 n”这个条件隐式定义的。它的导数来自隐函数定理：对这个条件求微分，再解一个小线性方程组得到 δλ。论文附录 C 做的正是这件事，只是手推。在 JAX 里，这一步对应一个自定义求根规则（例如 <code class="language-plaintext highlighter-rouge">jax.lax.custom_root</code>），框架不会对内循环的迭代过程求导。</p>

<p>一个用 AD 求 Jacobian 的最小 Newton 循环：</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">jax</span>
<span class="kn">import</span> <span class="nn">jax.numpy</span> <span class="k">as</span> <span class="n">jnp</span>
<span class="n">jax</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="s">"jax_enable_x64"</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">newton_fixed_point</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">,</span> <span class="n">max_iter</span><span class="o">=</span><span class="mi">50</span><span class="p">):</span>
    <span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">g</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span>
    <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">max_iter</span><span class="p">):</span>
        <span class="n">fx</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">norm</span><span class="p">(</span><span class="n">fx</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">tol</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">it</span>
        <span class="n">J</span> <span class="o">=</span> <span class="n">jax</span><span class="p">.</span><span class="n">jacfwd</span><span class="p">(</span><span class="n">f</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>                 <span class="c1"># 精确，= dg/dx - I
</span>        <span class="n">step</span> <span class="o">=</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">solve</span><span class="p">(</span><span class="n">J</span><span class="p">,</span> <span class="o">-</span><span class="n">fx</span><span class="p">)</span>
        <span class="n">t</span> <span class="o">=</span> <span class="mf">1.0</span>                              <span class="c1"># 回溯线搜索
</span>        <span class="k">while</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">norm</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">t</span> <span class="o">*</span> <span class="n">step</span><span class="p">))</span> <span class="o">&gt;</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">norm</span><span class="p">(</span><span class="n">fx</span><span class="p">)</span> <span class="ow">and</span> <span class="n">t</span> <span class="o">&gt;</span> <span class="mf">1e-4</span><span class="p">:</span>
            <span class="n">t</span> <span class="o">*=</span> <span class="mf">0.5</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">t</span> <span class="o">*</span> <span class="n">step</span>
    <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">max_iter</span>
</code></pre></div></div>

<p>不管是手推还是用 AD 求 Jacobian，都有两个常见的坑：</p>

<ul>
  <li><strong>本征值简并。</strong> 一阶微扰论要除以能量差 E_j − E_j′。局域问题的基态简并，或者两条能带在高对称 k 点交叉时，单个本征矢的导数没有定义。AD 解决不了这个问题，只会给出 NaN 或 inf。应该对仍然有良好定义的量求导（整个简并子空间的投影算符，或展宽后的密度矩阵），而不是对单个本征矢求导。</li>
  <li><strong>收敛到哪个根。</strong> Newton 法收敛到离初值最近的根：可能是金属解，可能是 R = 0 的绝缘解，也可能是鞍点。要检查能量，并和从相邻 U 延拓过来的解比较。</li>
</ul>

<h2 id="什么时候不该用-newton-法">什么时候不该用 Newton 法</h2>

<p>未知数很多时（完整的 R 矩阵、多格点、与 DFT 电荷自洽），哪怕是精确的 Jacobian，构造和分解也太贵。有两种折中：</p>

<ul>
  <li><strong>拟 Newton 混合</strong>（Broyden、Pulay/DIIS、Anderson）用历史步构造近似的逆 Jacobian。大多数 DFT 程序都用它，通常是从线性混合升级的第一选择。</li>
  <li><strong>Newton–Krylov</strong> 不显式构造 J，而是迭代求解 Newton 方程，只需要 Jacobian 和向量的乘积；前向 AD 求一次这种乘积的代价是一次循环的几倍（<a href="https://doi.org/10.1016/j.jcp.2003.08.010">Knoll and Keyes 2004</a>）。</li>
</ul>

<p>一个合理的默认做法：离根较远时用拟 Newton 混合；接近根或接近相变时（慢方向让所有混合方法都爬得很慢），换成用精确（或 Krylov）Jacobian 的 Newton 步。</p>

<h2 id="机器学习里的同一个问题">机器学习里的同一个问题</h2>

<p><strong>线性混合就是梯度下降。</strong> 把混合步写成 x_{k+1} = x_k − λ (x_k − g(x_k))。如果 x − g(x) 恰好是某个损失函数 L 的梯度，这就是学习率为 λ 的梯度下降。在不动点附近，”Hessian” 是 H = I − J，本征值为 1 − μ。上面两个方向的例子于是就是在曲率为 0.05 和 1.5 的二次函数上做梯度下降，条件数 κ = 1.5/0.05 = 30。最好的步长 λ = 2/(0.05 + 1.5) ≈ 1.29 正是教科书里的最优学习率，每步 0.935 的缩小因子正是教科书里的界 (κ − 1)/(κ + 1) = 29/31。一个慢方向加一个振荡方向，就是病态（ill-conditioned）问题，而病态问题不是调学习率能解决的。</p>

<p><strong>排斥不动点就是鞍点。</strong> 本征值 μ &gt; 1 对应负曲率 1 − μ &lt; 0，所以在损失函数的图像里，这个不动点是鞍点（或极大值）。从随机初值出发的梯度下降几乎必然不会收敛到严格鞍点（<a href="https://arxiv.org/abs/1602.04915">Lee et al. 2016</a>），线性混合落不到这种解上也是同一个机制。Newton 法对所有非退化驻点一视同仁，会被鞍点吸引，这是深度学习不用朴素 Newton 法的原因之一（<a href="https://arxiv.org/abs/1406.2572">Dauphin et al. 2014</a>）。同一个事实，两个领域的需求正好相反：在深度学习里鞍点是陷阱，在自洽问题里这个”鞍点”可能正是要找的物理解，这是 Newton 法的价值所在，也是上文要检查能量的原因。</p>

<p>这个类比只在 J 对称、x − g(x) 真的是梯度时才严格成立。Gutzwiller 映射一般不对称，J 还可能有复本征值（误差边转边缩），梯度下降里没有对应的情形。不过每个方向的因子 1 − λ(1 − μ) 在哪种情况下都成立。</p>

<p>深度平衡模型（Deep Equilibrium Models，<a href="https://arxiv.org/abs/1909.01377">Bai, Kolter and Koltun 2019</a>）把一层的输出定义为不动点 z = f(z, x)，用 Broyden 或 Anderson 迭代求解，并用隐函数定理求梯度，而不是对迭代过程反向传播。数学上它和上面 λ 的求导是同一件事：一个不动点、隐函数定理，以及在精确和近似 Jacobian 之间的取舍。</p>

<p>后续：<a href="/2026/11/26/adam-vs-newton-zh/">Adam 和 Newton 都在调步长，差在哪</a></p>

<h2 id="参考文献">参考文献</h2>

<p>J. Zhang, M.-F. Tian, G.-X. Jin, Y.-F. Xu and X. Dai, “Implementation of LDA+Gutzwiller with Newton’s method”, <em>Chinese Physics B</em> <strong>26</strong>, 017103 (2017), <a href="https://doi.org/10.1088/1674-1056/26/1/017103">doi:10.1088/1674-1056/26/1/017103</a>.</p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="computational-physics" /><summary type="html"><![CDATA[不动点映射只要有一个本征值接近 1，线性混合就要走几百步；有一个大于 1，它就根本找不到这个解。Newton 法怎么同时解决这两个问题，Jacobian 从哪来，以及什么时候不该用它。]]></summary></entry><entry><title type="html">Why linear mixing stalls, and what Newton’s method fixes: Gutzwiller self-consistency as a worked example</title><link href="https://thomaszhang2661.github.io/2026/11/23/newton-gutzwiller-self-consistency/" rel="alternate" type="text/html" title="Why linear mixing stalls, and what Newton’s method fixes: Gutzwiller self-consistency as a worked example" /><published>2026-11-23T00:00:00+00:00</published><updated>2026-11-23T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/23/newton-gutzwiller-self-consistency</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/23/newton-gutzwiller-self-consistency/"><![CDATA[<p><em>Tag: computational physics. A side note from my earlier physics work; most of this blog is about LLM inference.</em></p>

<p><a href="/2026/11/23/newton-gutzwiller-self-consistency-zh/">中文版</a></p>

<p>Most self-consistent methods in electronic structure (DFT, DFT+U, DFT+DMFT, the Gutzwiller approximation) end in the
same loop: guess some parameters, compute new ones from them, repeat until input equals output. The default way to
run that loop is linear mixing, and the default complaint is that it is slow near the interesting physics, or
does not converge at all.</p>

<p>In 2017 my coauthors and I replaced linear mixing with Newton’s method in an LDA+Gutzwiller code
(<a href="https://doi.org/10.1088/1674-1056/26/1/017103">Chinese Physics B 26, 017103</a>). This post explains the idea
without the material-specific details: why linear mixing slows down, why it can miss solutions entirely, what
Newton’s method changes, and what the Jacobian costs.</p>

<h2 id="the-loop">The loop</h2>

<p>In the Gutzwiller approximation, each correlated orbital α gets two numbers: a quasiparticle weight R_α (between 0
and 1; it narrows the band) and an on-site energy shift E_α (it moves the band). The loop has two halves:</p>

<ol>
  <li><strong>Single-particle half.</strong> Given R and E, build the renormalized band Hamiltonian, diagonalize it at every
k-point, and read off the orbital occupations n_α and the kinetic-energy derivatives ∂E_kin/∂R_α.</li>
  <li><strong>Many-body half.</strong> Given n and ∂E_kin/∂R, build a small Hamiltonian on the local Fock states, find its ground
state, and read off new values of R and E. Inside this half there is an inner loop: local Lagrange
multipliers λ_α must be tuned until the local occupations match n_α.</li>
</ol>

<p>Put the two halves together and you get a map x_out = g(x_in), where x collects all R_α and E_α. A solution is a
fixed point, g(x) = x.</p>

<p>Nothing below depends on the Gutzwiller details. Any self-consistent loop fits the same picture.</p>

<h2 id="linear-mixing-and-its-speed-limit">Linear mixing and its speed limit</h2>

<p>Linear mixing feeds back a fraction of the output:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x_{k+1} = x_k + λ (g(x_k) − x_k),      0 &lt; λ ≤ 1
</code></pre></div></div>

<p>Close to the fixed point x<em>, write the error as e_k = x_k − x</em>, and let J = ∂g/∂x at x*. To first order,</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>e_{k+1} = (I + λ (J − I)) e_k
</code></pre></div></div>

<p>So each eigenvector of J is its own channel. If J has eigenvalue μ along some direction, the error along that direction
is multiplied by <strong>1 − λ(1 − μ)</strong> at every step. Everything about linear mixing follows from this one factor.</p>

<p><strong>A small example.</strong> Suppose J has two eigenvalues, μ_A = 0.95 and μ_B = −0.5.</p>

<ul>
  <li>With λ = 1 (plain iteration), channel A shrinks by 0.95 per step. Getting its error down by 10⁻⁶ takes
ln(10⁻⁶)/ln(0.95) ≈ 270 steps.</li>
  <li>Channel A alone would want a large λ: λ = 1/(1 − 0.95) = 20 removes it in one step.</li>
  <li>But channel B’s factor is 1 − 1.5λ, and it must stay above −1, so λ &lt; 4/3. At λ = 20 channel B is multiplied
by −29 per step and the iteration blows up, oscillating.</li>
  <li>The best compromise sets the two factors equal in size: λ = 2/1.55 ≈ 1.29, and both channels then shrink by
0.935 per step. That is still ≈ 208 steps.</li>
</ul>

<p>One slow direction and one oscillating direction together set a hard limit, and tuning λ can’t get past it.
Linear mixing is slow whenever J has eigenvalues spread over a wide range.</p>

<p>The same thing happens in a nonlinear toy: a two-variable map g whose Jacobian at the fixed point has exactly these
eigenvalues, with the two directions rotated so they mix, and a quadratic term on top. From the same start, linear
mixing needs 273 steps at λ = 1 and 228 at λ = 1.29 to cut the error by 10⁻⁶, and blows up at λ = 1.4. Newton’s
method needs 4 (panel a).</p>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 740 440" width="100%" role="img" aria-label="Error per step on a 2-variable toy fixed point" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Noto Sans SC', Helvetica, Arial, sans-serif;max-width:740px">
<rect width="740" height="440" fill="#fcfcfb" />
<text x="20" y="32" font-size="19" font-weight="600" fill="#0b0b0b">Error per step on a 2-variable toy fixed point</text>
<text x="20" y="54" font-size="12.5" fill="#52514e">g(x) with Jacobian eigenvalues at the fixed point as marked; same start; |x − x*| on a log scale</text>
<text x="70" y="96" font-size="13.5" font-weight="600" fill="#0b0b0b">(a) μ = 0.95 and −0.5</text>
<line x1="70" x2="370" y1="380.0" y2="380.0" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="384.0" font-size="11.5" fill="#7a7974" text-anchor="end">1e-14</text>
<line x1="70" x2="370" y1="316.5" y2="316.5" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="320.5" font-size="11.5" fill="#7a7974" text-anchor="end">1e-10</text>
<line x1="70" x2="370" y1="252.9" y2="252.9" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="256.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e-6</text>
<line x1="70" x2="370" y1="189.4" y2="189.4" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="193.4" font-size="11.5" fill="#7a7974" text-anchor="end">1e-2</text>
<line x1="70" x2="370" y1="125.9" y2="125.9" stroke="#e4e3df" stroke-width="1" />
<text x="64" y="129.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e2</text>
<text x="70.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">0</text>
<text x="170.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">100</text>
<text x="270.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">200</text>
<text x="370.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">300</text>
<line x1="70" x2="370" y1="380" y2="380" stroke="#7a7974" stroke-width="1" />
<text x="220.0" y="416" font-size="12" fill="#52514e" text-anchor="middle">step</text>
<path d="M70.0,168.0 L71.0,171.5 L72.0,172.6 L73.0,173.3 L74.0,173.5 L75.0,173.8 L76.0,173.9 L77.0,174.1 L78.0,174.3 L79.0,174.5 L80.0,174.7 L81.0,174.9 L82.0,175.1 L83.0,175.3 L84.0,175.5 L85.0,175.7 L86.0,175.9 L87.0,176.1 L88.0,176.3 L89.0,176.6 L90.0,176.8 L91.0,177.0 L92.0,177.3 L93.0,177.5 L94.0,177.7 L95.0,178.0 L96.0,178.2 L97.0,178.5 L98.0,178.8 L99.0,179.0 L100.0,179.3 L101.0,179.6 L102.0,179.8 L103.0,180.1 L104.0,180.4 L105.0,180.7 L106.0,181.0 L107.0,181.3 L108.0,181.6 L109.0,181.8 L110.0,182.1 L111.0,182.4 L112.0,182.7 L113.0,183.0 L114.0,183.4 L115.0,183.7 L116.0,184.0 L117.0,184.3 L118.0,184.6 L119.0,184.9 L120.0,185.2 L121.0,185.5 L122.0,185.9 L123.0,186.2 L124.0,186.5 L125.0,186.8 L126.0,187.2 L127.0,187.5 L128.0,187.8 L129.0,188.1 L130.0,188.5 L131.0,188.8 L132.0,189.1 L133.0,189.5 L134.0,189.8 L135.0,190.2 L136.0,190.5 L137.0,190.8 L138.0,191.2 L139.0,191.5 L140.0,191.8 L141.0,192.2 L142.0,192.5 L143.0,192.9 L144.0,193.2 L145.0,193.5 L146.0,193.9 L147.0,194.2 L148.0,194.6 L149.0,194.9 L150.0,195.3 L151.0,195.6 L152.0,196.0 L153.0,196.3 L154.0,196.7 L155.0,197.0 L156.0,197.3 L157.0,197.7 L158.0,198.0 L159.0,198.4 L160.0,198.7 L161.0,199.1 L162.0,199.4 L163.0,199.8 L164.0,200.1 L165.0,200.5 L166.0,200.8 L167.0,201.2 L168.0,201.5 L169.0,201.9 L170.0,202.2 L171.0,202.6 L172.0,202.9 L173.0,203.3 L174.0,203.6 L175.0,204.0 L176.0,204.3 L177.0,204.7 L178.0,205.1 L179.0,205.4 L180.0,205.8 L181.0,206.1 L182.0,206.5 L183.0,206.8 L184.0,207.2 L185.0,207.5 L186.0,207.9 L187.0,208.2 L188.0,208.6 L189.0,208.9 L190.0,209.3 L191.0,209.6 L192.0,210.0 L193.0,210.3 L194.0,210.7 L195.0,211.0 L196.0,211.4 L197.0,211.7 L198.0,212.1 L199.0,212.5 L200.0,212.8 L201.0,213.2 L202.0,213.5 L203.0,213.9 L204.0,214.2 L205.0,214.6 L206.0,214.9 L207.0,215.3 L208.0,215.6 L209.0,216.0 L210.0,216.3 L211.0,216.7 L212.0,217.0 L213.0,217.4 L214.0,217.8 L215.0,218.1 L216.0,218.5 L217.0,218.8 L218.0,219.2 L219.0,219.5 L220.0,219.9 L221.0,220.2 L222.0,220.6 L223.0,220.9 L224.0,221.3 L225.0,221.6 L226.0,222.0 L227.0,222.4 L228.0,222.7 L229.0,223.1 L230.0,223.4 L231.0,223.8 L232.0,224.1 L233.0,224.5 L234.0,224.8 L235.0,225.2 L236.0,225.5 L237.0,225.9 L238.0,226.2 L239.0,226.6 L240.0,226.9 L241.0,227.3 L242.0,227.7 L243.0,228.0 L244.0,228.4 L245.0,228.7 L246.0,229.1 L247.0,229.4 L248.0,229.8 L249.0,230.1 L250.0,230.5 L251.0,230.8 L252.0,231.2 L253.0,231.5 L254.0,231.9 L255.0,232.3 L256.0,232.6 L257.0,233.0 L258.0,233.3 L259.0,233.7 L260.0,234.0 L261.0,234.4 L262.0,234.7 L263.0,235.1 L264.0,235.4 L265.0,235.8 L266.0,236.1 L267.0,236.5 L268.0,236.9 L269.0,237.2 L270.0,237.6 L271.0,237.9 L272.0,238.3 L273.0,238.6 L274.0,239.0 L275.0,239.3 L276.0,239.7 L277.0,240.0 L278.0,240.4 L279.0,240.7 L280.0,241.1 L281.0,241.5 L282.0,241.8 L283.0,242.2 L284.0,242.5 L285.0,242.9 L286.0,243.2 L287.0,243.6 L288.0,243.9 L289.0,244.3 L290.0,244.6 L291.0,245.0 L292.0,245.3 L293.0,245.7 L294.0,246.1 L295.0,246.4 L296.0,246.8 L297.0,247.1 L298.0,247.5 L299.0,247.8 L300.0,248.2 L301.0,248.5 L302.0,248.9 L303.0,249.2 L304.0,249.6 L305.0,249.9 L306.0,250.3 L307.0,250.7 L308.0,251.0 L309.0,251.4 L310.0,251.7 L311.0,252.1 L312.0,252.4 L313.0,252.8 L314.0,253.1 L315.0,253.5 L316.0,253.8 L317.0,254.2 L318.0,254.5 L319.0,254.9 L320.0,255.3 L321.0,255.6 L322.0,256.0 L323.0,256.3 L324.0,256.7 L325.0,257.0 L326.0,257.4 L327.0,257.7 L328.0,258.1 L329.0,258.4 L330.0,258.8 L331.0,259.1 L332.0,259.5 L333.0,259.9 L334.0,260.2 L335.0,260.6 L336.0,260.9 L337.0,261.3 L338.0,261.6 L339.0,262.0 L340.0,262.3 L341.0,262.7 L342.0,263.0 L343.0,263.4 L344.0,263.7 L345.0,264.1 L346.0,264.5 L347.0,264.8 L348.0,265.2 L349.0,265.5 L350.0,265.9 L351.0,266.2 L352.0,266.6 L353.0,266.9 L354.0,267.3 L355.0,267.6 L356.0,268.0 L357.0,268.3 L358.0,268.7 L359.0,269.0 L360.0,269.4 L361.0,269.8 L362.0,270.1 L363.0,270.5 L364.0,270.8 L365.0,271.2 L366.0,271.5 L367.0,271.9 L368.0,272.2 L369.0,272.6 L370.0,272.9" fill="none" stroke="#2a78d6" stroke-width="1.8" stroke-linejoin="round"><title>linear mixing λ = 1</title></path>
<path d="M70.0,168.0 L71.0,168.9 L72.0,168.5 L73.0,169.4 L74.0,169.0 L75.0,169.8 L76.0,169.5 L77.0,170.2 L78.0,170.0 L79.0,170.6 L80.0,170.4 L81.0,171.0 L82.0,170.9 L83.0,171.4 L84.0,171.3 L85.0,171.8 L86.0,171.8 L87.0,172.3 L88.0,172.2 L89.0,172.7 L90.0,172.7 L91.0,173.2 L92.0,173.2 L93.0,173.7 L94.0,173.7 L95.0,174.2 L96.0,174.3 L97.0,174.7 L98.0,174.9 L99.0,175.3 L100.0,175.4 L101.0,175.9 L102.0,176.1 L103.0,176.5 L104.0,176.7 L105.0,177.1 L106.0,177.4 L107.0,177.8 L108.0,178.0 L109.0,178.5 L110.0,178.7 L111.0,179.2 L112.0,179.5 L113.0,179.9 L114.0,180.2 L115.0,180.6 L116.0,181.0 L117.0,181.4 L118.0,181.7 L119.0,182.2 L120.0,182.5 L121.0,183.0 L122.0,183.3 L123.0,183.8 L124.0,184.2 L125.0,184.6 L126.0,185.0 L127.0,185.4 L128.0,185.8 L129.0,186.3 L130.0,186.7 L131.0,187.1 L132.0,187.5 L133.0,188.0 L134.0,188.4 L135.0,188.8 L136.0,189.3 L137.0,189.7 L138.0,190.1 L139.0,190.6 L140.0,191.0 L141.0,191.5 L142.0,191.9 L143.0,192.4 L144.0,192.8 L145.0,193.2 L146.0,193.7 L147.0,194.1 L148.0,194.6 L149.0,195.0 L150.0,195.5 L151.0,195.9 L152.0,196.4 L153.0,196.8 L154.0,197.3 L155.0,197.7 L156.0,198.2 L157.0,198.6 L158.0,199.1 L159.0,199.6 L160.0,200.0 L161.0,200.5 L162.0,200.9 L163.0,201.4 L164.0,201.8 L165.0,202.3 L166.0,202.7 L167.0,203.2 L168.0,203.7 L169.0,204.1 L170.0,204.6 L171.0,205.0 L172.0,205.5 L173.0,205.9 L174.0,206.4 L175.0,206.9 L176.0,207.3 L177.0,207.8 L178.0,208.2 L179.0,208.7 L180.0,209.2 L181.0,209.6 L182.0,210.1 L183.0,210.5 L184.0,211.0 L185.0,211.4 L186.0,211.9 L187.0,212.4 L188.0,212.8 L189.0,213.3 L190.0,213.7 L191.0,214.2 L192.0,214.7 L193.0,215.1 L194.0,215.6 L195.0,216.0 L196.0,216.5 L197.0,217.0 L198.0,217.4 L199.0,217.9 L200.0,218.3 L201.0,218.8 L202.0,219.3 L203.0,219.7 L204.0,220.2 L205.0,220.6 L206.0,221.1 L207.0,221.6 L208.0,222.0 L209.0,222.5 L210.0,222.9 L211.0,223.4 L212.0,223.9 L213.0,224.3 L214.0,224.8 L215.0,225.2 L216.0,225.7 L217.0,226.2 L218.0,226.6 L219.0,227.1 L220.0,227.5 L221.0,228.0 L222.0,228.5 L223.0,228.9 L224.0,229.4 L225.0,229.8 L226.0,230.3 L227.0,230.8 L228.0,231.2 L229.0,231.7 L230.0,232.1 L231.0,232.6 L232.0,233.1 L233.0,233.5 L234.0,234.0 L235.0,234.4 L236.0,234.9 L237.0,235.4 L238.0,235.8 L239.0,236.3 L240.0,236.7 L241.0,237.2 L242.0,237.7 L243.0,238.1 L244.0,238.6 L245.0,239.0 L246.0,239.5 L247.0,240.0 L248.0,240.4 L249.0,240.9 L250.0,241.3 L251.0,241.8 L252.0,242.3 L253.0,242.7 L254.0,243.2 L255.0,243.6 L256.0,244.1 L257.0,244.6 L258.0,245.0 L259.0,245.5 L260.0,245.9 L261.0,246.4 L262.0,246.9 L263.0,247.3 L264.0,247.8 L265.0,248.2 L266.0,248.7 L267.0,249.2 L268.0,249.6 L269.0,250.1 L270.0,250.5 L271.0,251.0 L272.0,251.5 L273.0,251.9 L274.0,252.4 L275.0,252.8 L276.0,253.3 L277.0,253.8 L278.0,254.2 L279.0,254.7 L280.0,255.1 L281.0,255.6 L282.0,256.1 L283.0,256.5 L284.0,257.0 L285.0,257.4 L286.0,257.9 L287.0,258.4 L288.0,258.8 L289.0,259.3 L290.0,259.7 L291.0,260.2 L292.0,260.7 L293.0,261.1 L294.0,261.6 L295.0,262.0 L296.0,262.5 L297.0,263.0 L298.0,263.4 L299.0,263.9 L300.0,264.3 L301.0,264.8 L302.0,265.3 L303.0,265.7 L304.0,266.2 L305.0,266.6 L306.0,267.1 L307.0,267.6 L308.0,268.0 L309.0,268.5 L310.0,268.9 L311.0,269.4 L312.0,269.9 L313.0,270.3 L314.0,270.8 L315.0,271.2 L316.0,271.7 L317.0,272.2 L318.0,272.6 L319.0,273.1 L320.0,273.5 L321.0,274.0 L322.0,274.5 L323.0,274.9 L324.0,275.4 L325.0,275.8 L326.0,276.3 L327.0,276.8 L328.0,277.2 L329.0,277.7 L330.0,278.1 L331.0,278.6 L332.0,279.1 L333.0,279.5 L334.0,280.0 L335.0,280.4 L336.0,280.9 L337.0,281.4 L338.0,281.8 L339.0,282.3 L340.0,282.7 L341.0,283.2 L342.0,283.7 L343.0,284.1 L344.0,284.6 L345.0,285.0 L346.0,285.5 L347.0,286.0 L348.0,286.4 L349.0,286.9 L350.0,287.3 L351.0,287.8 L352.0,288.3 L353.0,288.7 L354.0,289.2 L355.0,289.6 L356.0,290.1 L357.0,290.6 L358.0,291.0 L359.0,291.5 L360.0,291.9 L361.0,292.4 L362.0,292.9 L363.0,293.3 L364.0,293.8 L365.0,294.2 L366.0,294.7 L367.0,295.2 L368.0,295.6 L369.0,296.1 L370.0,296.5" fill="none" stroke="#1baf7a" stroke-width="1.8" stroke-linejoin="round"><title>linear mixing λ = 1.29</title></path>
<path d="M70.0,168.0 L71.0,168.0 L72.0,166.7 L73.0,166.8 L74.0,165.3 L75.0,165.6 L76.0,163.9 L77.0,164.4 L78.0,162.5 L79.0,163.2 L80.0,161.1 L81.0,162.0 L82.0,159.7 L83.0,160.8 L84.0,158.2 L85.0,159.5 L86.0,156.5 L87.0,158.0 L88.0,154.5 L89.0,155.9 L90.0,151.6 L91.0,152.5 L92.0,146.6 L93.0,145.3 L94.0,134.4 L95.0,122.9 L96.0,110.0" fill="none" stroke="#eb6834" stroke-width="1.8" stroke-linejoin="round"><title>linear mixing λ = 1.4</title></path>
<text x="100.0" y="122.0" font-size="11" fill="#eb6834">diverges</text>
<path d="M70.0,168.0 L71.0,185.0 L72.0,195.4 L73.0,222.0 L74.0,274.7 L75.0,380.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round"><title>Newton</title></path>
<circle cx="70.0" cy="168.0" r="3" fill="#0b0b0b" />
<circle cx="71.0" cy="185.0" r="3" fill="#0b0b0b" />
<circle cx="72.0" cy="195.4" r="3" fill="#0b0b0b" />
<circle cx="73.0" cy="222.0" r="3" fill="#0b0b0b" />
<circle cx="74.0" cy="274.7" r="3" fill="#0b0b0b" />
<circle cx="75.0" cy="380.0" r="3" fill="#0b0b0b" />
<rect x="220" y="312" width="12" height="3" fill="#2a78d6" />
<text x="238" y="317" font-size="11.5" fill="#0b0b0b">linear mixing λ = 1</text>
<rect x="220" y="328" width="12" height="3" fill="#1baf7a" />
<text x="238" y="333" font-size="11.5" fill="#0b0b0b">linear mixing λ = 1.29</text>
<rect x="220" y="344" width="12" height="3" fill="#eb6834" />
<text x="238" y="349" font-size="11.5" fill="#0b0b0b">linear mixing λ = 1.4</text>
<rect x="220" y="360" width="12" height="3" fill="#0b0b0b" />
<text x="238" y="365" font-size="11.5" fill="#0b0b0b">Newton</text>
<text x="430" y="96" font-size="13.5" font-weight="600" fill="#0b0b0b">(b) μ = 1.2 and 0.5 (repelling)</text>
<line x1="430" x2="720" y1="380.0" y2="380.0" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="384.0" font-size="11.5" fill="#7a7974" text-anchor="end">1e-14</text>
<line x1="430" x2="720" y1="316.5" y2="316.5" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="320.5" font-size="11.5" fill="#7a7974" text-anchor="end">1e-10</text>
<line x1="430" x2="720" y1="252.9" y2="252.9" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="256.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e-6</text>
<line x1="430" x2="720" y1="189.4" y2="189.4" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="193.4" font-size="11.5" fill="#7a7974" text-anchor="end">1e-2</text>
<line x1="430" x2="720" y1="125.9" y2="125.9" stroke="#e4e3df" stroke-width="1" />
<text x="424" y="129.9" font-size="11.5" fill="#7a7974" text-anchor="end">1e2</text>
<text x="430.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">0</text>
<text x="502.5" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">100</text>
<text x="575.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">200</text>
<text x="647.5" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">300</text>
<text x="720.0" y="398" font-size="11.5" fill="#7a7974" text-anchor="middle">400</text>
<line x1="430" x2="720" y1="380" y2="380" stroke="#7a7974" stroke-width="1" />
<text x="575.0" y="416" font-size="12" fill="#52514e" text-anchor="middle">step</text>
<path d="M430.0,168.0 L430.7,169.9 L431.4,170.1 L432.2,169.1 L432.9,167.7 L433.6,166.2 L434.4,164.5 L435.1,162.6 L435.8,160.6 L436.5,158.4 L437.2,155.8 L438.0,152.5 L438.7,148.2 L439.4,142.0 L440.1,131.6 L440.9,112.2 L441.6,110.0" fill="none" stroke="#2a78d6" stroke-width="1.8" stroke-linejoin="round"><title>linear mixing λ = 1</title></path>
<text x="445.6" y="122.0" font-size="11" fill="#2a78d6">diverges</text>
<path d="M430.0,168.0 L430.7,168.2 L431.4,168.4 L432.2,168.6 L432.9,168.7 L433.6,168.9 L434.4,169.1 L435.1,169.2 L435.8,169.3 L436.5,169.4 L437.2,169.5 L438.0,169.6 L438.7,169.7 L439.4,169.7 L440.1,169.8 L440.9,169.8 L441.6,169.8 L442.3,169.8 L443.1,169.7 L443.8,169.7 L444.5,169.7 L445.2,169.6 L445.9,169.5 L446.7,169.4 L447.4,169.4 L448.1,169.3 L448.9,169.2 L449.6,169.0 L450.3,168.9 L451.0,168.8 L451.8,168.7 L452.5,168.5 L453.2,168.4 L453.9,168.2 L454.6,168.1 L455.4,167.9 L456.1,167.8 L456.8,167.6 L457.6,167.5 L458.3,167.3 L459.0,167.1 L459.7,167.0 L460.4,166.8 L461.2,166.6 L461.9,166.4 L462.6,166.2 L463.4,166.1 L464.1,165.9 L464.8,165.7 L465.5,165.5 L466.2,165.3 L467.0,165.1 L467.7,164.9 L468.4,164.7 L469.1,164.5 L469.9,164.3 L470.6,164.1 L471.3,163.9 L472.1,163.7 L472.8,163.5 L473.5,163.3 L474.2,163.1 L474.9,162.8 L475.7,162.6 L476.4,162.4 L477.1,162.2 L477.9,161.9 L478.6,161.7 L479.3,161.4 L480.0,161.2 L480.8,160.9 L481.5,160.7 L482.2,160.4 L482.9,160.2 L483.6,159.9 L484.4,159.6 L485.1,159.3 L485.8,159.0 L486.6,158.7 L487.3,158.4 L488.0,158.1 L488.7,157.8 L489.4,157.5 L490.2,157.1 L490.9,156.8 L491.6,156.4 L492.4,156.1 L493.1,155.7 L493.8,155.3 L494.5,154.8 L495.2,154.4 L496.0,153.9 L496.7,153.4 L497.4,152.9 L498.1,152.4 L498.9,151.8 L499.6,151.2 L500.3,150.5 L501.1,149.8 L501.8,149.0 L502.5,148.1 L503.2,147.2 L503.9,146.1 L504.7,144.8 L505.4,143.4 L506.1,141.6 L506.9,139.4 L507.6,136.6 L508.3,132.8 L509.0,127.0 L509.8,117.4 L510.5,110.0" fill="none" stroke="#1baf7a" stroke-width="1.8" stroke-linejoin="round"><title>linear mixing λ = 0.1</title></path>
<text x="514.5" y="122.0" font-size="11" fill="#1baf7a">diverges</text>
<path d="M430.0,168.0 L430.7,168.0 L431.4,168.0 L432.2,168.0 L432.9,168.1 L433.6,168.1 L434.4,168.1 L435.1,168.1 L435.8,168.1 L436.5,168.2 L437.2,168.2 L438.0,168.2 L438.7,168.2 L439.4,168.2 L440.1,168.3 L440.9,168.3 L441.6,168.3 L442.3,168.3 L443.1,168.3 L443.8,168.4 L444.5,168.4 L445.2,168.4 L445.9,168.4 L446.7,168.4 L447.4,168.4 L448.1,168.5 L448.9,168.5 L449.6,168.5 L450.3,168.5 L451.0,168.5 L451.8,168.6 L452.5,168.6 L453.2,168.6 L453.9,168.6 L454.6,168.6 L455.4,168.6 L456.1,168.7 L456.8,168.7 L457.6,168.7 L458.3,168.7 L459.0,168.7 L459.7,168.7 L460.4,168.8 L461.2,168.8 L461.9,168.8 L462.6,168.8 L463.4,168.8 L464.1,168.8 L464.8,168.9 L465.5,168.9 L466.2,168.9 L467.0,168.9 L467.7,168.9 L468.4,168.9 L469.1,168.9 L469.9,169.0 L470.6,169.0 L471.3,169.0 L472.1,169.0 L472.8,169.0 L473.5,169.0 L474.2,169.0 L474.9,169.1 L475.7,169.1 L476.4,169.1 L477.1,169.1 L477.9,169.1 L478.6,169.1 L479.3,169.1 L480.0,169.2 L480.8,169.2 L481.5,169.2 L482.2,169.2 L482.9,169.2 L483.6,169.2 L484.4,169.2 L485.1,169.2 L485.8,169.3 L486.6,169.3 L487.3,169.3 L488.0,169.3 L488.7,169.3 L489.4,169.3 L490.2,169.3 L490.9,169.3 L491.6,169.3 L492.4,169.4 L493.1,169.4 L493.8,169.4 L494.5,169.4 L495.2,169.4 L496.0,169.4 L496.7,169.4 L497.4,169.4 L498.1,169.4 L498.9,169.4 L499.6,169.5 L500.3,169.5 L501.1,169.5 L501.8,169.5 L502.5,169.5 L503.2,169.5 L503.9,169.5 L504.7,169.5 L505.4,169.5 L506.1,169.5 L506.9,169.5 L507.6,169.5 L508.3,169.6 L509.0,169.6 L509.8,169.6 L510.5,169.6 L511.2,169.6 L511.9,169.6 L512.6,169.6 L513.4,169.6 L514.1,169.6 L514.8,169.6 L515.5,169.6 L516.3,169.6 L517.0,169.6 L517.7,169.6 L518.5,169.6 L519.2,169.6 L519.9,169.7 L520.6,169.7 L521.4,169.7 L522.1,169.7 L522.8,169.7 L523.5,169.7 L524.2,169.7 L525.0,169.7 L525.7,169.7 L526.4,169.7 L527.1,169.7 L527.9,169.7 L528.6,169.7 L529.3,169.7 L530.0,169.7 L530.8,169.7 L531.5,169.7 L532.2,169.7 L533.0,169.7 L533.7,169.7 L534.4,169.7 L535.1,169.7 L535.9,169.7 L536.6,169.7 L537.3,169.7 L538.0,169.7 L538.8,169.7 L539.5,169.7 L540.2,169.7 L540.9,169.7 L541.6,169.7 L542.4,169.7 L543.1,169.7 L543.8,169.7 L544.5,169.7 L545.3,169.7 L546.0,169.7 L546.7,169.7 L547.5,169.7 L548.2,169.7 L548.9,169.7 L549.6,169.7 L550.4,169.7 L551.1,169.7 L551.8,169.7 L552.5,169.7 L553.2,169.7 L554.0,169.7 L554.7,169.7 L555.4,169.7 L556.1,169.7 L556.9,169.7 L557.6,169.7 L558.3,169.7 L559.0,169.7 L559.8,169.7 L560.5,169.7 L561.2,169.7 L562.0,169.7 L562.7,169.7 L563.4,169.7 L564.1,169.7 L564.9,169.7 L565.6,169.7 L566.3,169.7 L567.0,169.7 L567.8,169.7 L568.5,169.7 L569.2,169.7 L569.9,169.6 L570.6,169.6 L571.4,169.6 L572.1,169.6 L572.8,169.6 L573.5,169.6 L574.3,169.6 L575.0,169.6 L575.7,169.6 L576.5,169.6 L577.2,169.6 L577.9,169.6 L578.6,169.6 L579.4,169.6 L580.1,169.6 L580.8,169.6 L581.5,169.6 L582.2,169.6 L583.0,169.5 L583.7,169.5 L584.4,169.5 L585.1,169.5 L585.9,169.5 L586.6,169.5 L587.3,169.5 L588.0,169.5 L588.8,169.5 L589.5,169.5 L590.2,169.5 L591.0,169.5 L591.7,169.5 L592.4,169.5 L593.1,169.4 L593.9,169.4 L594.6,169.4 L595.3,169.4 L596.0,169.4 L596.8,169.4 L597.5,169.4 L598.2,169.4 L598.9,169.4 L599.6,169.4 L600.4,169.4 L601.1,169.3 L601.8,169.3 L602.5,169.3 L603.3,169.3 L604.0,169.3 L604.7,169.3 L605.5,169.3 L606.2,169.3 L606.9,169.3 L607.6,169.3 L608.4,169.3 L609.1,169.2 L609.8,169.2 L610.5,169.2 L611.2,169.2 L612.0,169.2 L612.7,169.2 L613.4,169.2 L614.1,169.2 L614.9,169.2 L615.6,169.1 L616.3,169.1 L617.0,169.1 L617.8,169.1 L618.5,169.1 L619.2,169.1 L620.0,169.1 L620.7,169.1 L621.4,169.1 L622.1,169.0 L622.9,169.0 L623.6,169.0 L624.3,169.0 L625.0,169.0 L625.8,169.0 L626.5,169.0 L627.2,169.0 L627.9,169.0 L628.6,168.9 L629.4,168.9 L630.1,168.9 L630.8,168.9 L631.5,168.9 L632.3,168.9 L633.0,168.9 L633.7,168.9 L634.5,168.8 L635.2,168.8 L635.9,168.8 L636.6,168.8 L637.4,168.8 L638.1,168.8 L638.8,168.8 L639.5,168.8 L640.2,168.7 L641.0,168.7 L641.7,168.7 L642.4,168.7 L643.1,168.7 L643.9,168.7 L644.6,168.7 L645.3,168.6 L646.0,168.6 L646.8,168.6 L647.5,168.6 L648.2,168.6 L649.0,168.6 L649.7,168.6 L650.4,168.6 L651.1,168.5 L651.9,168.5 L652.6,168.5 L653.3,168.5 L654.0,168.5 L654.8,168.5 L655.5,168.5 L656.2,168.4 L656.9,168.4 L657.6,168.4 L658.4,168.4 L659.1,168.4 L659.8,168.4 L660.5,168.4 L661.3,168.3 L662.0,168.3 L662.7,168.3 L663.5,168.3 L664.2,168.3 L664.9,168.3 L665.6,168.3 L666.4,168.2 L667.1,168.2 L667.8,168.2 L668.5,168.2 L669.2,168.2 L670.0,168.2 L670.7,168.1 L671.4,168.1 L672.1,168.1 L672.9,168.1 L673.6,168.1 L674.3,168.1 L675.0,168.1 L675.8,168.0 L676.5,168.0 L677.2,168.0 L678.0,168.0 L678.7,168.0 L679.4,168.0 L680.1,167.9 L680.9,167.9 L681.6,167.9 L682.3,167.9 L683.0,167.9 L683.8,167.9 L684.5,167.9 L685.2,167.8 L685.9,167.8 L686.6,167.8 L687.4,167.8 L688.1,167.8 L688.8,167.8 L689.5,167.7 L690.3,167.7 L691.0,167.7 L691.7,167.7 L692.5,167.7 L693.2,167.7 L693.9,167.6 L694.6,167.6 L695.4,167.6 L696.1,167.6 L696.8,167.6 L697.5,167.6 L698.2,167.6 L699.0,167.5 L699.7,167.5 L700.4,167.5 L701.2,167.5 L701.9,167.5 L702.6,167.5 L703.3,167.4 L704.0,167.4 L704.8,167.4 L705.5,167.4 L706.2,167.4 L707.0,167.4 L707.7,167.3 L708.4,167.3 L709.1,167.3 L709.8,167.3 L710.6,167.3 L711.3,167.3 L712.0,167.2 L712.8,167.2 L713.5,167.2 L714.2,167.2 L714.9,167.2 L715.6,167.2 L716.4,167.1 L717.1,167.1 L717.8,167.1 L718.5,167.1 L719.3,167.1 L720.0,167.0" fill="none" stroke="#eb6834" stroke-width="1.8" stroke-linejoin="round"><title>linear mixing λ = 0.01</title></path>
<path d="M430.0,168.0 L430.7,181.7 L431.4,208.9 L432.2,257.5 L432.9,351.8 L433.6,380.0" fill="none" stroke="#0b0b0b" stroke-width="2.4" stroke-linejoin="round"><title>Newton</title></path>
<circle cx="430.0" cy="168.0" r="3" fill="#0b0b0b" />
<circle cx="430.7" cy="181.7" r="3" fill="#0b0b0b" />
<circle cx="431.4" cy="208.9" r="3" fill="#0b0b0b" />
<circle cx="432.2" cy="257.5" r="3" fill="#0b0b0b" />
<circle cx="432.9" cy="351.8" r="3" fill="#0b0b0b" />
<circle cx="433.6" cy="380.0" r="3" fill="#0b0b0b" />
<rect x="570" y="312" width="12" height="3" fill="#2a78d6" />
<text x="588" y="317" font-size="11.5" fill="#0b0b0b">linear mixing λ = 1</text>
<rect x="570" y="328" width="12" height="3" fill="#1baf7a" />
<text x="588" y="333" font-size="11.5" fill="#0b0b0b">linear mixing λ = 0.1</text>
<rect x="570" y="344" width="12" height="3" fill="#eb6834" />
<text x="588" y="349" font-size="11.5" fill="#0b0b0b">linear mixing λ = 0.01</text>
<rect x="570" y="360" width="12" height="3" fill="#0b0b0b" />
<text x="588" y="365" font-size="11.5" fill="#0b0b0b">Newton</text>
<text transform="translate(16,245.0) rotate(-90)" font-size="12" fill="#52514e" text-anchor="middle">|x − x*|</text>
</svg>

<p><strong>Why the slow direction appears near a transition.</strong> At a phase transition or a branch point, the solution changes
character, and at that point the root of f(x) = g(x) − x becomes degenerate: ∂f/∂x = J − I has a zero eigenvalue,
so J has an eigenvalue equal to 1. Approaching the transition, μ → 1 and the factor 1 − λ(1 − μ) → 1, for any λ. This
is critical slowing down, seen in the solver. In the 2017 paper the effect shows directly in SrVO₃: far from the
Brinkman–Rice transition (U = 1 eV) both methods converge within a few steps, while near it (U = 6 eV) the
linear-mixing energy is still creeping after 60 steps.</p>

<p><strong>Solutions that linear mixing cannot find.</strong> If J has a real eigenvalue μ &gt; 1, the factor 1 + λ(μ − 1) is above 1 for
every λ &gt; 0. That fixed point repels linear mixing, however small the mixing, so the iteration never lands on it.
Near the Brinkman–Rice transition the Gutzwiller equations have two solutions with different R
(<a href="https://doi.org/10.1103/PhysRevB.85.035133">Lanatà et al. 2012</a>). In our runs linear mixing never found the branch
with small R, and Newton’s method did. A repelling fixed point is a likely explanation. Panel (b) shows it in the toy: with eigenvalues 1.2 and 0.5, linear
mixing moves away from the solution at λ = 1, 0.1 and 0.01, while Newton’s method converges in 4 steps.</p>

<h2 id="newtons-method">Newton’s method</h2>

<p>Newton’s method stops treating the loop as an iteration and treats it as root finding, f(x) = g(x) − x = 0:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>x_{k+1} = x_k − (J_f)⁻¹ f(x_k),      J_f = ∂f/∂x = ∂g/∂x − I
</code></pre></div></div>

<p>(The paper writes the Jacobian as ∂x_out/∂x_in. In the update the matrix is ∂g/∂x − I, because f = g − x.)</p>

<p>Two things change.</p>

<ul>
  <li><strong>Convergence no longer depends on how the eigenvalues are spread out.</strong> Near a nondegenerate root, Newton’s error
roughly squares at every step: 10⁻² → 10⁻⁴ → 10⁻⁸. Slow channels and oscillating channels are treated alike.
For the two-channel example above, if the map were exactly linear, Newton would land on the root in one step.</li>
  <li><strong>The iteration does not need to be stable at the fixed point.</strong> Newton converges to any root where J_f is
invertible, including ones with μ &gt; 1 that linear mixing can never reach.</li>
</ul>

<p>In the paper, Newton’s method converged in 4–6 outer steps for SrVO₃. For La₂O₃Fe₂Se₂, with 16 correlated atoms in
the magnetic cell, linear mixing in the inner loop never found λ that met the
occupation constraints, and Newton found them in 6 steps.</p>

<p>Newton’s method has two costs.</p>

<p><strong>It needs a good starting point.</strong> Far from the root, a full Newton step can overshoot. The standard remedy is a
line search: keep the Newton direction and choose the step length. We used a golden-section search along the Newton
line; backtracking (halve the step until the residual |f| decreases) is the simpler common choice. A cap on the step
size also helps, because R and E have different scales (R ~ 1, E in eV).</p>

<p><strong>It needs the Jacobian.</strong> This is the real cost.</p>

<h2 id="where-the-jacobian-comes-from">Where the Jacobian comes from</h2>

<p>With N correlated spin-orbitals there are 2N unknowns (R_α and E_α). For La₂O₃Fe₂Se₂ that is up to 16 atoms × 10
spin-orbitals × 2 = 320 numbers before symmetry is used. There are three ways to get J_f.</p>

<ol>
  <li><strong>Finite differences.</strong> Perturb each input, rerun the loop, take the difference. That costs 2N + 1 = 321 full loops
per Newton step, more than an entire linear-mixing run. Accuracy also depends on choosing the step size.</li>
  <li><strong>By hand, with the chain rule.</strong> We did this in 2017. Split J along the two halves of the loop: derivatives of
(n, ∂E_kin/∂R) with respect to (R, E) from the band problem, then derivatives of (R, E) with respect to
(n, ∂E_kin/∂R) from the many-body problem, and multiply. Eigenvector derivatives come from first-order
perturbation theory. In the paper this takes three appendices of formulas.</li>
  <li><strong>Automatic differentiation.</strong> Write g once in JAX or PyTorch and let the framework produce J exactly. This is
what I would do today.</li>
</ol>

<p>The inner loop needs the most care in any of the three. The multipliers λ are not computed by a formula. They are
defined implicitly, by the condition that the local occupations N(λ; n, ∂E_kin/∂R) equal n. Their derivatives come
from the implicit function theorem: differentiate the condition, then solve a small linear system for δλ. The
paper’s Appendix C does exactly this, by hand. In JAX the same step is a custom root rule (for example
<code class="language-plaintext highlighter-rouge">jax.lax.custom_root</code>), and the framework never differentiates through the inner iterations.</p>

<p>A minimal Newton loop with an AD Jacobian:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">jax</span>
<span class="kn">import</span> <span class="nn">jax.numpy</span> <span class="k">as</span> <span class="n">jnp</span>
<span class="n">jax</span><span class="p">.</span><span class="n">config</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="s">"jax_enable_x64"</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">newton_fixed_point</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">tol</span><span class="o">=</span><span class="mf">1e-10</span><span class="p">,</span> <span class="n">max_iter</span><span class="o">=</span><span class="mi">50</span><span class="p">):</span>
    <span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">g</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="n">x</span>
    <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">max_iter</span><span class="p">):</span>
        <span class="n">fx</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">norm</span><span class="p">(</span><span class="n">fx</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">tol</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">it</span>
        <span class="n">J</span> <span class="o">=</span> <span class="n">jax</span><span class="p">.</span><span class="n">jacfwd</span><span class="p">(</span><span class="n">f</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>                 <span class="c1"># exact, = dg/dx - I
</span>        <span class="n">step</span> <span class="o">=</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">solve</span><span class="p">(</span><span class="n">J</span><span class="p">,</span> <span class="o">-</span><span class="n">fx</span><span class="p">)</span>
        <span class="n">t</span> <span class="o">=</span> <span class="mf">1.0</span>                              <span class="c1"># backtracking line search
</span>        <span class="k">while</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">norm</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">t</span> <span class="o">*</span> <span class="n">step</span><span class="p">))</span> <span class="o">&gt;</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">t</span><span class="p">)</span> <span class="o">*</span> <span class="n">jnp</span><span class="p">.</span><span class="n">linalg</span><span class="p">.</span><span class="n">norm</span><span class="p">(</span><span class="n">fx</span><span class="p">)</span> <span class="ow">and</span> <span class="n">t</span> <span class="o">&gt;</span> <span class="mf">1e-4</span><span class="p">:</span>
            <span class="n">t</span> <span class="o">*=</span> <span class="mf">0.5</span>
        <span class="n">x</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">t</span> <span class="o">*</span> <span class="n">step</span>
    <span class="k">return</span> <span class="n">x</span><span class="p">,</span> <span class="n">max_iter</span>
</code></pre></div></div>

<p>Two pitfalls are common to the hand-derived and AD Jacobians:</p>

<ul>
  <li><strong>Degenerate eigenvalues.</strong> First-order perturbation theory divides by energy differences E_j − E_j′. When the
ground state of the local problem is degenerate, or two bands cross at a high-symmetry k-point, the derivative
of a single eigenvector is undefined. AD does not fix this; it returns NaN or inf. Differentiate quantities that
stay well defined (projectors onto a whole degenerate subspace, or the smeared density matrix), not single
eigenvectors.</li>
  <li><strong>Which root you get.</strong> Newton converges to whatever root is nearby: the metallic solution, the R = 0 insulating
one, or a saddle point. Check the energy, and compare against a solution continued from a nearby U.</li>
</ul>

<h2 id="when-not-to-use-newtons-method">When not to use Newton’s method</h2>

<p>When there are many unknowns (full R matrices, many sites, charge self-consistency with DFT), even an exact
Jacobian is too expensive to build and factor. Two middle grounds exist:</p>

<ul>
  <li><strong>Quasi-Newton mixing</strong> (Broyden, Pulay/DIIS, Anderson) builds an approximate inverse Jacobian from the history
of past steps. Most DFT codes use it, and it is usually the right first upgrade from linear mixing.</li>
  <li><strong>Newton–Krylov</strong> never forms J. It solves the Newton system iteratively and needs only Jacobian-vector products,
which forward-mode AD gives at a small multiple of the cost of one loop evaluation each
(<a href="https://doi.org/10.1016/j.jcp.2003.08.010">Knoll and Keyes 2004</a>).</li>
</ul>

<p>A reasonable default: use quasi-Newton mixing far from the root, then switch to Newton steps with an exact
(or Krylov) Jacobian near it, or near a transition, where the slow direction makes every mixing scheme crawl.</p>

<h2 id="the-same-problem-in-machine-learning">The same problem in machine learning</h2>

<p><strong>Linear mixing is gradient descent.</strong> Write the mixing step as x_{k+1} = x_k − λ (x_k − g(x_k)). If x − g(x) were
the gradient of some loss L, this would be gradient descent with learning rate λ. Near the fixed point the “Hessian”
is H = I − J, with eigenvalues 1 − μ. The two-channel example above is then gradient descent on a quadratic with
curvatures 0.05 and 1.5, condition number κ = 1.5/0.05 = 30. The best step, λ = 2/(0.05 + 1.5) ≈ 1.29, is the
textbook optimal learning rate, and the factor 0.935 per step is the textbook bound (κ − 1)/(κ + 1) = 29/31.
A slow channel next to an oscillating one is an ill-conditioned problem, and no learning rate fixes ill-conditioning.</p>

<p><strong>A repulsive fixed point is a saddle.</strong> An eigenvalue μ &gt; 1 gives a negative curvature 1 − μ &lt; 0, so in the
loss picture that fixed point is a saddle (or a maximum). Gradient descent from a random start almost surely does
not converge to a strict saddle (<a href="https://arxiv.org/abs/1602.04915">Lee et al. 2016</a>), the same mechanism that keeps
linear mixing off such a solution. Newton’s method treats all nondegenerate stationary points alike and is
attracted to saddles, one reason deep learning avoids plain Newton
(<a href="https://arxiv.org/abs/1406.2572">Dauphin et al. 2014</a>). The two fields want opposite things from the same fact:
in deep learning a saddle is a trap, while in a self-consistency problem the “saddle” may be the physical solution
you are looking for, and that is what Newton buys. It is also why the energy check above matters.</p>

<p>The analogy is exact only when J is symmetric, so that x − g(x) really is a gradient. The Gutzwiller map in general is
not symmetric, and J can have complex eigenvalues (rotating errors), which have no counterpart in gradient descent.
The per-channel factor 1 − λ(1 − μ) holds either way.</p>

<p>Deep equilibrium models (<a href="https://arxiv.org/abs/1909.01377">Bai, Kolter and Koltun 2019</a>) define a layer’s output as
a fixed point z = f(z, x), solve it with Broyden or Anderson iterations, and get gradients from the implicit
function theorem, not by backpropagating through the iterations. Mathematically this is the same as the λ
derivatives above: a fixed point, the implicit function theorem, and a choice between exact and approximate Jacobians.</p>

<p>Follow-up: <a href="/2026/11/26/adam-vs-newton/">Adam and Newton both rescale the step. What’s the difference?</a></p>

<h2 id="reference">Reference</h2>

<p>J. Zhang, M.-F. Tian, G.-X. Jin, Y.-F. Xu and X. Dai, “Implementation of LDA+Gutzwiller with Newton’s method”,
<em>Chinese Physics B</em> <strong>26</strong>, 017103 (2017), <a href="https://doi.org/10.1088/1674-1056/26/1/017103">doi:10.1088/1674-1056/26/1/017103</a>.</p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="computational-physics" /><summary type="html"><![CDATA[One eigenvalue of the fixed-point map near 1 makes linear mixing take hundreds of steps, and one above 1 hides a solution from it entirely. How Newton's method fixes both, where the Jacobian comes from, and when not to use it.]]></summary></entry><entry xml:lang="zh-CN"><title type="html">跑一次不算结果：在 vLLM 里比较 KV cache 格式</title><link href="https://thomaszhang2661.github.io/2026/11/14/one-run-is-not-a-result-zh/" rel="alternate" type="text/html" title="跑一次不算结果：在 vLLM 里比较 KV cache 格式" /><published>2026-11-14T00:00:00+00:00</published><updated>2026-11-14T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/14/one-run-is-not-a-result-zh</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/14/one-run-is-not-a-result-zh/"><![CDATA[<p><a href="/2026/11/14/one-run-is-not-a-result/">English version</a></p>

<p>我在 vLLM 里用 MATH-500 比较一种量化 KV cache 和 BF16。同一个模型、同样的 500 道题、贪心解码。第一次运行的结果是量化 cache 掉了 1.2 个点。</p>

<p>然后我把 BF16 又跑了两次，除了题目顺序什么都没改：</p>

<table>
  <thead>
    <tr>
      <th>运行</th>
      <th>正确率（BF16 KV）</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>原顺序</td>
      <td>68.4%</td>
    </tr>
    <tr>
      <td>打乱 1</td>
      <td>67.0%</td>
    </tr>
    <tr>
      <td>打乱 2</td>
      <td>67.6%</td>
    </tr>
  </tbody>
</table>

<p>DeepSeek-R1-Distill-Llama-8B，vLLM 0.33，H200，最多生成 4096 个 token。光是基线自己就差了 1.4 个点，比量化的差距还大；任意两次运行之间约有 47 道题对错不同。每个配置跑三次取平均后，量化 cache 的差距从 −1.2 变成 +0.07 个点（配对置换检验 p = 1.0）。</p>

<p>这个模型属于波动大的：约 30% 的回答撞到 4096 token 的上限，被截断的思维链很容易往任一边倒。Qwen2.5-7B-Instruct 上，BF16 跑五次只在 71.0–71.6% 之间。但量化配置比 BF16 波动更大：同样在 Qwen2.5-7B-Instruct 上，一个量化配置在 200 道题上一次 67.0%、另一次 73.5%（批的组成和 GPU 节点不同）。</p>

<h2 id="为什么同一个配置会得到不同的答案">为什么同一个配置会得到不同的答案</h2>

<p><strong>1. 贪心解码“本来应该”是确定的。</strong> 贪心解码每一步都选概率最大的 token。同样的输入、同样的模型，按理说每次都该选出一模一样的 token。</p>

<p><strong>2. 计算机里的加法，换个顺序结果就可能不同。</strong> 计算机存的数位数有限，每做一次加法都要舍入，所以先加哪个、后加哪个，结果可能不一样。假设只能保留 3 位有效数字：</p>

<ul>
  <li>先算 1000 + 4 = 1004，舍入成 1000；再加 4，还是 1000。</li>
  <li>先算 4 + 4 = 8，再算 1000 + 8 = 1008，舍入成 1010。</li>
</ul>

<p>同样是 1000、4、4 三个数相加，一个得 1000，一个得 1010。BF16 只有大约 2–3 位有效数字；GPU 算子内部累加一般用 FP32（约 7 位），差别小得多，但依然存在。</p>

<p><strong>3. 加法的顺序，取决于“这一批里有多少个请求”。</strong> 推理服务会把很多请求放在一起成批计算。GPU 算子为了跑得快，会按批的大小选不同的算法：批小的时候，把一次很长的求和（矩阵乘法里沿隐藏维度，注意力里沿上下文）拆成几段并行算再合并；批大的时候换一种分块方式。拆法不同，加法的顺序就不同。所以同一个请求，和不同的请求放在一批里，算出来的数值会有极小的差别，比如概率在小数点后第四、五位不一样。“批不变”的意思是：不管和谁一起算，同一个请求的结果都完全一样。现在的大多数算子做不到这一点，Thinking Machines 指出这才是结果不确定的主要原因。</p>

<p><strong>4. 打乱题目顺序，“和谁一批”就变了。</strong> 我只打乱了题目顺序，别的什么都没改。但 vLLM 是连续成批的：有请求算完，新的请求就补进来。顺序一变，每一步里同时在算哪些题、一批有多大，也就跟着变了。</p>

<p><strong>5. 极小的差别，怎么变成答错题。</strong> 大部分时候，概率最大的 token 远远领先第二名，小数点后第五位的差别改变不了结果。但偶尔会遇到两个 token 的概率几乎一样（比如 0.4001 对 0.4000），这时那一点点差别就足以让选中的 token 翻转。每个 token 都依赖前面所有的 token，一个 token 不同，后面整段生成都会走上另一条路。推理模型一次要生成几千个 token，总有某一步碰上这种“几乎平局”，于是思维链分岔，最后的答案可能变对，也可能变错。</p>

<p>这就是同一个配置跑三次，正确率是 68.4%、67.0%、67.6% 的原因。</p>

<p><strong>前人的工作。</strong> <a href="https://thinkingmachines.ai/blog/defeating-nondeterminism-in-llm-inference/">Thinking Machines</a>（Horace He，2025）：根本原因是算子不是批不变的；他们写了批不变的算子，让 vLLM 跑出了完全确定的结果，代价是慢一些。Yuan 等人（<a href="https://arxiv.org/abs/2506.09501">arXiv 2506.09501</a>，初版题为 “Give Me FP32 or Give Me Death?”）：BF16 的有效位数太少，最容易出现这种翻转；改用 FP32 计算，差别小到几乎不会改变选择。他们测到，在 BF16 下只改批大小、GPU 数量和型号，R1-Distill-Qwen-7B 的正确率就能差到 9%。</p>

<h2 id="对比较-kv-cache-格式意味着什么">对比较 KV cache 格式意味着什么</h2>

<p>量化 KV cache 本身也会带来很小的数值差别，大小和这种运行之间的波动差不多，也会触发同样的分岔。所以只跑一次、看到差了两三个点，根本分不清是量化造成的，还是这次碰巧分岔了。</p>

<p>我现在的做法：</p>

<ol>
  <li>每个配置至少跑三次，每次打乱题目顺序。</li>
  <li>用全部题目，不用子集。</li>
  <li>逐题配对比较，用符号翻转置换检验，而不是只看平均数。</li>
  <li>报告差距时，同时给出基线自己的运行间波动。</li>
</ol>

<p>这件事也让我撤回过一个结论：根据 200 道题上的单次运行，我曾报告某个配置掉了 5 个点（p = 0.011）。换成 500 道题、跑五次后，差距变成 −1.9 个点（p = 0.17），我把它撤回了。</p>

<hr />

<p><em>更正（2026 年 11 月 15 日）：本文第一版里 R1-Distill-Llama-8B 的数字，是在分词器加载出错（<a href="https://github.com/huggingface/transformers/issues/45488">transformers #45488</a>）的情况下测的：vLLM 里的提示丢了空格和换行。表格和正文的数字已换成修好分词器后重跑的结果。BF16 三次的差距从 2.8 个点变成 1.4 个点，任意两次运行之间仍约有 47 道题对错不同；量化 cache 单次差 −1.2，三次平均 +0.07。结论不变。Qwen2.5 的数字不受影响。</em></p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="kv-cache" /><category term="benchmarking" /><category term="vllm" /><summary type="html"><![CDATA[同一个 BF16 配置、同样的 500 道 MATH-500 题，只打乱顺序：68.4%、67.0%、67.6%。为什么单次贪心解码比较不了 KV cache 格式，以及我现在怎么做。]]></summary></entry><entry><title type="html">One run is not a result: comparing KV-cache dtypes in vLLM</title><link href="https://thomaszhang2661.github.io/2026/11/14/one-run-is-not-a-result/" rel="alternate" type="text/html" title="One run is not a result: comparing KV-cache dtypes in vLLM" /><published>2026-11-14T00:00:00+00:00</published><updated>2026-11-14T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/14/one-run-is-not-a-result</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/14/one-run-is-not-a-result/"><![CDATA[<p><a href="/2026/11/14/one-run-is-not-a-result-zh/">中文版</a></p>

<p>I was comparing a quantized KV cache against BF16 on MATH-500 in vLLM. Same model, same 500 problems, greedy
decoding. The first run said the quantized cache lost 1.2 points.</p>

<p>Then I reran BF16 twice more, changing nothing but the order of the problems:</p>

<table>
  <thead>
    <tr>
      <th>Run</th>
      <th>Accuracy (BF16 KV)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Original order</td>
      <td>68.4%</td>
    </tr>
    <tr>
      <td>Shuffle 1</td>
      <td>67.0%</td>
    </tr>
    <tr>
      <td>Shuffle 2</td>
      <td>67.6%</td>
    </tr>
  </tbody>
</table>

<p>DeepSeek-R1-Distill-Llama-8B, vLLM 0.33, H200, up to 4,096 new tokens. The baseline alone moves by 1.4 points, more than the gap, and
any two runs disagree on about 47 of the 500 problems. Averaged over three runs each, the quantized cache’s gap
goes from −1.2 to +0.07 points (paired permutation test, p = 1.0).</p>

<p>This model is on the noisy end: about 30% of its answers hit the 4,096-token cap, and a truncated chain of thought is
easy to tip either way. On Qwen2.5-7B-Instruct, five BF16 runs spanned only 71.0–71.6%. But quantized configurations
move more than BF16: on Qwen2.5-7B-Instruct, one quantized configuration scored 67.0% on 200 problems in one job
and 73.5% in another (different batch composition and GPU node).</p>

<h2 id="why-the-same-config-gives-different-answers">Why the same config gives different answers</h2>

<p><strong>1. Greedy decoding should be deterministic.</strong> At each step it picks the most likely token. Same input, same model:
in principle, the same tokens every time.</p>

<p><strong>2. Floating-point addition depends on order.</strong> A computer keeps a fixed number of digits and rounds after every
addition, so the order of a sum can change the result. With 3 significant digits:</p>

<ul>
  <li>(1000 + 4) + 4: 1004 rounds to 1000, and 1000 + 4 rounds to 1000 again.</li>
  <li>1000 + (4 + 4): 1000 + 8 = 1008 rounds to 1010.</li>
</ul>

<p>Same three numbers, two results. BF16 keeps only about 2–3 significant digits. The FP32 accumulators inside GPU
kernels keep about 7, so the differences there are smaller, but still there.</p>

<p><strong>3. The order of a sum depends on the batch.</strong> A serving engine computes many requests together. To stay fast, GPU
kernels choose their strategy by batch size: with a small batch, one long sum (over the hidden dimension in a
matmul, over the context in attention) is split into chunks that run in parallel and are then combined; with a large
batch, the work is tiled differently. Different splits, different order of additions. So the same request, batched
with different other requests, gets slightly different numbers, say in the fourth or fifth decimal of a probability.
A kernel is <em>batch-invariant</em> if a request’s result does not depend on what else is in its batch. Most kernels in
today’s engines are not, and Thinking Machines identify this as the main cause.</p>

<p><strong>4. Shuffling the problems changes the batches.</strong> I changed only the order of the problems. But vLLM batches
continuously: as requests finish, new ones join. A different order means each problem shares its steps with
different problems, in batches of different sizes.</p>

<p><strong>5. A tiny difference becomes a different answer.</strong> Usually the top token leads by a wide margin, and a difference
in the fifth decimal changes nothing. But now and then two tokens are almost tied (0.4001 vs 0.4000), and the tiny
difference flips the choice. Every later token depends on all the earlier ones, so one flipped token sends the rest
of the generation down another path. A reasoning model writes thousands of tokens, so a near-tie somewhere is
likely; the chain of thought forks, and the final answer may turn right or wrong.</p>

<p>That is how one config scores 68.4%, 67.0% and 67.6%.</p>

<p><strong>Prior work.</strong> <a href="https://thinkingmachines.ai/blog/defeating-nondeterminism-in-llm-inference/">Thinking Machines</a> (Horace He, 2025): the root cause is kernels that are not batch-invariant; with batch-invariant
kernels they got fully deterministic output from vLLM, at some cost in speed. Yuan et al. (<a href="https://arxiv.org/abs/2506.09501">arXiv 2506.09501</a>, first titled
“Give Me FP32 or Give Me Death?”): BF16’s few digits make such flips likely, and computing in FP32 makes the
differences small enough that the choice almost never flips. They measured up to 9% accuracy swings for
R1-Distill-Qwen-7B under BF16 from batch size, GPU count and GPU type alone.</p>

<h2 id="what-this-means-for-kv-cache-comparisons">What this means for KV-cache comparisons</h2>

<p>A quantized KV cache also changes the numbers slightly, by about as much as this noise, and so triggers the same
forks. If one run shows a 2–3 point gap, you cannot tell whether the quantization caused it or the run happened to
fork differently.</p>

<p>What I do now:</p>

<ol>
  <li>Run each configuration at least three times, shuffling the problem order.</li>
  <li>Use all problems, not a subset.</li>
  <li>Compare per problem, paired across configurations, and test with a sign-flip permutation test, not by eyeballing
averages.</li>
  <li>Report the baseline’s own run-to-run spread next to the gap.</li>
</ol>

<p>It also cost me a claim: from single runs on 200 problems I had reported a 5-point loss for one configuration
(p = 0.011). Over 500 problems and five runs it became −1.9 points (p = 0.17), and I withdrew it.</p>

<hr />

<p><em>Correction (Nov 15, 2026): The first version of this post used R1-Distill-Llama-8B numbers measured with a
tokenizer-loading bug (<a href="https://github.com/huggingface/transformers/issues/45488">transformers #45488</a>) that removed
spaces and newlines from the prompts in vLLM. The table and the numbers in the text now come from reruns with the
correct tokenizer. The BF16 runs now spread over 1.4 points instead of 2.8, while any two runs still disagree on about
47 of the 500 problems, and the quantized cache’s single-run gap of −1.2 averages to +0.07 over three runs. The
conclusion is unchanged. The Qwen2.5 numbers were not affected.</em></p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="kv-cache" /><category term="benchmarking" /><category term="vllm" /><summary type="html"><![CDATA[Same BF16 config, same 500 MATH-500 problems, only the order shuffled: 68.4%, 67.0%, 67.6%. Why single greedy runs can't compare KV-cache dtypes, and what I do instead.]]></summary></entry><entry xml:lang="zh-CN"><title type="html">vLLM 的 FP8 KV cache 会让小号 Qwen2 系推理模型悄悄“变傻”，一行配置就能自查</title><link href="https://thomaszhang2661.github.io/2026/11/13/fp8-kv-cache-qwen2-vllm-zh/" rel="alternate" type="text/html" title="vLLM 的 FP8 KV cache 会让小号 Qwen2 系推理模型悄悄“变傻”，一行配置就能自查" /><published>2026-11-13T00:00:00+00:00</published><updated>2026-11-13T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/13/fp8-kv-cache-qwen2-vllm-zh</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/13/fp8-kv-cache-qwen2-vllm-zh/"><![CDATA[<p><a href="/2026/11/13/fp8-kv-cache-qwen2-vllm/">English version</a></p>

<p><strong>一句话总结</strong></p>
<ul>
  <li>用 vLLM 默认的 FP8 KV cache，7B 及以下的 Qwen2 系模型做长推理会崩：R1-Distill-Qwen-7B 在 MATH-500 上从 71% 掉到 0.5%，不报错也没有警告。</li>
  <li>同样的 R1 蒸馏换成 Llama 底座就没事（67.0% 对 68.4%），14B 的 Qwen 模型也没事。</li>
  <li>原因在第 0 层：Qwen2 的 key 偏置让这一层的 K 几乎全是均值，FP8 的舍入误差落在注意力真正用到的那一小部分上。一行配置就能自查和修好：<code class="language-plaintext highlighter-rouge">kv_cache_dtype_skip_layers=["0"]</code>。</li>
</ul>

<p>在 vLLM 里打开 <code class="language-plaintext highlighter-rouge">kv_cache_dtype="fp8"</code>，用 DeepSeek-R1-Distill-Qwen-7B 做数学题，正确率从 71% 掉到 0.5%。不报错，也没有警告，模型只是开始不停地重复：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>To convert convert convert the point point point ...
</code></pre></div></div>

<p>下面是实测结果、原因和修法。</p>

<h2 id="数据">数据</h2>

<p>vLLM 0.33.0，H200，MATH-500 前 200 题，贪心解码，最多生成 4096 个 token，FP8（E4M3）KV cache，用 vLLM 默认的（未标定的）缩放因子。</p>

<table>
  <thead>
    <tr>
      <th>模型</th>
      <th>BF16 KV</th>
      <th>FP8 KV</th>
      <th>FP8，第 0 层保持 BF16</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>DeepSeek-R1-Distill-Qwen-7B</td>
      <td>71.0%</td>
      <td>0.5%</td>
      <td>71.5%</td>
    </tr>
    <tr>
      <td>Qwen2.5-7B-Instruct</td>
      <td>73.5%</td>
      <td>1.5%</td>
      <td>72.0%</td>
    </tr>
    <tr>
      <td>DeepSeek-R1-Distill-Qwen-1.5B</td>
      <td>61.0%</td>
      <td>0.0%</td>
      <td>61.0%</td>
    </tr>
    <tr>
      <td>DeepSeek-R1-Distill-Qwen-14B</td>
      <td>70.0%</td>
      <td>73.0%</td>
      <td>–</td>
    </tr>
    <tr>
      <td>DeepSeek-R1-Distill-Llama-8B（全部 500 题）</td>
      <td>68.4%</td>
      <td>67.0%</td>
      <td>68.8%</td>
    </tr>
  </tbody>
</table>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 740 460" width="100%" role="img" aria-label="vLLM 中 MATH-500 正确率：FP8 KV cache 对 BF16" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Noto Sans SC', Helvetica, Arial, sans-serif;max-width:740px">
<rect width="740" height="460" fill="#fcfcfb" />
<text x="64" y="34" font-size="20" font-weight="600" fill="#0b0b0b">vLLM 中 MATH-500 正确率：FP8 KV cache 对 BF16</text>
<text x="64" y="58" font-size="13" fill="#52514e">vLLM 0.33.0，H200，贪心解码，最多 4096 token，默认 FP8 缩放因子</text>
<rect x="64" y="76" width="12" height="12" rx="3" fill="#2a78d6" />
<text x="82" y="86.5" font-size="13" fill="#0b0b0b">BF16 KV</text>
<rect x="199.39999999999998" y="76" width="12" height="12" rx="3" fill="#eb6834" />
<text x="217.39999999999998" y="86.5" font-size="13" fill="#0b0b0b">FP8 KV</text>
<rect x="326.59999999999997" y="76" width="12" height="12" rx="3" fill="#1baf7a" />
<text x="344.59999999999997" y="86.5" font-size="13" fill="#0b0b0b">FP8，第 0 层用 BF16</text>
<line x1="64" x2="716" y1="368.0" y2="368.0" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="372.0" font-size="12" fill="#7a7974" text-anchor="end">0%</text>
<line x1="64" x2="716" y1="305.5" y2="305.5" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="309.5" font-size="12" fill="#7a7974" text-anchor="end">20%</text>
<line x1="64" x2="716" y1="243.0" y2="243.0" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="247.0" font-size="12" fill="#7a7974" text-anchor="end">40%</text>
<line x1="64" x2="716" y1="180.5" y2="180.5" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="184.5" font-size="12" fill="#7a7974" text-anchor="end">60%</text>
<line x1="64" x2="716" y1="118.0" y2="118.0" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="122.0" font-size="12" fill="#7a7974" text-anchor="end">80%</text>
<path d="M88.2,368.0 V150.1 Q88.2,146.1 92.2,146.1 H110.2 Q114.2,146.1 114.2,150.1 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Qwen-7B · BF16 KV: 71.0%</title></path>
<path d="M116.2,368.0 V367.2 Q116.2,366.4 117.0,366.4 H141.4 Q142.2,366.4 142.2,367.2 V368.0 Z" fill="#eb6834"><title>R1-Distill-Qwen-7B · FP8 KV: 0.5%</title></path>
<text x="129.2" y="359.4" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">0.5%</text>
<path d="M144.2,368.0 V148.6 Q144.2,144.6 148.2,144.6 H166.2 Q170.2,144.6 170.2,148.6 V368.0 Z" fill="#1baf7a"><title>R1-Distill-Qwen-7B · FP8，第 0 层用 BF16: 71.5%</title></path>
<text x="129.2" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Qwen-7B</text>
<path d="M218.6,368.0 V142.3 Q218.6,138.3 222.6,138.3 H240.6 Q244.6,138.3 244.6,142.3 V368.0 Z" fill="#2a78d6"><title>Qwen2.5-7B-Instruct · BF16 KV: 73.5%</title></path>
<path d="M246.6,368.0 V365.7 Q246.6,363.3 248.9,363.3 H270.3 Q272.6,363.3 272.6,365.7 V368.0 Z" fill="#eb6834"><title>Qwen2.5-7B-Instruct · FP8 KV: 1.5%</title></path>
<text x="259.6" y="356.3" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">1.5%</text>
<path d="M274.6,368.0 V147.0 Q274.6,143.0 278.6,143.0 H296.6 Q300.6,143.0 300.6,147.0 V368.0 Z" fill="#1baf7a"><title>Qwen2.5-7B-Instruct · FP8，第 0 层用 BF16: 72.0%</title></path>
<text x="259.6" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">Qwen2.5-7B-Instruct</text>
<path d="M349.0,368.0 V181.4 Q349.0,177.4 353.0,177.4 H371.0 Q375.0,177.4 375.0,181.4 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Qwen-1.5B · BF16 KV: 61.0%</title></path>
<rect x="377.0" y="367.0" width="26" height="1" fill="#eb6834"><title>R1-Distill-Qwen-1.5B · FP8 KV: 0.0%</title></rect>
<text x="390.0" y="361.0" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">0.0%</text>
<path d="M405.0,368.0 V181.4 Q405.0,177.4 409.0,177.4 H427.0 Q431.0,177.4 431.0,181.4 V368.0 Z" fill="#1baf7a"><title>R1-Distill-Qwen-1.5B · FP8，第 0 层用 BF16: 61.0%</title></path>
<text x="390.0" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Qwen-1.5B</text>
<path d="M479.4,368.0 V153.2 Q479.4,149.2 483.4,149.2 H501.4 Q505.4,149.2 505.4,153.2 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Qwen-14B · BF16 KV: 70.0%</title></path>
<path d="M507.4,368.0 V143.9 Q507.4,139.9 511.4,139.9 H529.4 Q533.4,139.9 533.4,143.9 V368.0 Z" fill="#eb6834"><title>R1-Distill-Qwen-14B · FP8 KV: 73.0%</title></path>
<text x="520.4" y="132.9" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">73.0%</text>
<text x="548.4" y="362.0" font-size="11" fill="#7a7974" text-anchor="middle">未测</text>
<text x="520.4" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Qwen-14B</text>
<path d="M609.8,368.0 V158.2 Q609.8,154.2 613.8,154.2 H631.8 Q635.8,154.2 635.8,158.2 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Llama-8B · BF16 KV: 68.4%</title></path>
<path d="M637.8,368.0 V162.6 Q637.8,158.6 641.8,158.6 H659.8 Q663.8,158.6 663.8,162.6 V368.0 Z" fill="#eb6834"><title>R1-Distill-Llama-8B · FP8 KV: 67.0%</title></path>
<text x="650.8" y="151.6" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">67.0%</text>
<path d="M665.8,368.0 V157.0 Q665.8,153.0 669.8,153.0 H687.8 Q691.8,153.0 691.8,157.0 V368.0 Z" fill="#1baf7a"><title>R1-Distill-Llama-8B · FP8，第 0 层用 BF16: 68.8%</title></path>
<text x="650.8" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Llama-8B</text>
<path d="M74.0,402.0 v5 H575.6 v-5" fill="none" stroke="#7a7974" stroke-width="1" />
<text x="324.8" y="422.0" font-size="12" fill="#52514e" text-anchor="middle">Qwen2 底座</text>
<path d="M595.6,402.0 v5 H706.0 v-5" fill="none" stroke="#7a7974" stroke-width="1" />
<text x="650.8" y="422.0" font-size="12" fill="#52514e" text-anchor="middle">Llama 底座</text>
<text x="64" y="450" font-size="11.5" fill="#7a7974">前 200 题；R1-Distill-Llama-8B 为全部 500 题。完整数字见下表。</text>
</svg>

<p>换成只让<strong>最后一层</strong>保持 BF16，完全没用（R1-Distill-Qwen-7B，40 题上 0.0%）。14B 模型没事；我离线测困惑度，Qwen2.5-14B 和 Llama-3.1-8B 也不受影响。</p>

<p>最干净的对照是 DeepSeek-R1-Distill-Llama-8B：同样的 R1 蒸馏，底座换成没有 key 偏置的 Llama-3.1。全部层用 FP8 只差 1.4 个点（配对置换检验 p = 0.47，全部 500 题跑一次）。崩溃跟着 Qwen2 底座走，不是推理模型或 FP8 本身的问题。</p>

<h2 id="自查">自查</h2>

<p>同一个提示，把第 0 层排除在 FP8 之外再跑一次：</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span>
    <span class="n">model</span><span class="o">=</span><span class="s">"deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"</span><span class="p">,</span>
    <span class="n">kv_cache_dtype</span><span class="o">=</span><span class="s">"fp8"</span><span class="p">,</span>
    <span class="n">kv_cache_dtype_skip_layers</span><span class="o">=</span><span class="p">[</span><span class="s">"0"</span><span class="p">],</span>
<span class="p">)</span>
</code></pre></div></div>

<p>输出恢复正常，就是这个问题。代价是 28 层里有 1 层用 BF16。</p>

<p>注意：用 FlashAttention-3 后端时，有些模型会直接崩（<a href="https://github.com/vllm-project/vllm/issues/65821">vLLM #65821</a>，修复在 <a href="https://github.com/vllm-project/vllm/pull/65891">#65891</a>，还没合并）。加上 <code class="language-plaintext highlighter-rouge">attention_backend="FLASHINFER"</code> 就能绕开。</p>

<h2 id="为什么是第-0-层">为什么是第 0 层</h2>

<p>Qwen2 的 key 投影带偏置。到了第 0 层，偏置占了大头：各通道 K 均值的范数是 K 范数的 98%（Llama-3.1-8B 是 68%）。</p>

<p>注意力真正用到的，是 K 里<strong>随 token 变化</strong>的那一小部分。均值对同一个 query 的所有注意力分数加的是同一个数，softmax 会把它抵消掉。可 FP8 的舍入误差是按整个数值的大小算的，于是全落在了那一小部分上。好比站在山顶上量几个人的身高差，手里的尺子一格一百米。</p>

<p>整张量的缩放因子能防止大的数值溢出，但改变不了 E4M3 的相对精度，所以消不掉落在那一小部分上的舍入误差。何况第 0 层根本没有溢出：在 wikitext 上，它最大的 key 值是 203（R1-Distill-Qwen-7B）和 172（Qwen2.5-7B-Instruct），远低于 E4M3 的上限 448。（这里的正确率都用 vLLM 默认的缩放因子，我没有在 vLLM 里跑过标定后的缩放因子。）</p>

<p>而且第 0 层的误差不会只留在第 0 层。只把第 0 层量化成 FP8，最终输出的 KL 是中位数那一层的约 1300 倍（离线，R1-Distill-Qwen-7B，wikitext）。在长长的思维链里，误差一路累积，最后变成胡言乱语。离线测 Qwen2.5-7B 的 wikitext 困惑度：全部 FP8 从 6.31 涨到 78.2，保留第 0 层是 6.59。</p>

<h3 id="可-nvidia-说是最后一层">“可 NVIDIA 说是最后一层”</h3>

<p>NVIDIA 的 <a href="https://nvidia.github.io/TensorRT-Edge-LLM/latest/user_guide/features/FP8KV.html">TensorRT-Edge-LLM 文档</a>指出 Qwen2.5-7B-Instruct 用 FP8 KV 会明显掉点，原因是最后一层的数值很大。两件事都对：最后一层 K 的均值几乎和第 0 层一样大（均值的范数 240 对 274，中间各层约 12）。但数值大不等于敏感：只量化最后一层，KL 是中位数那一层的 8 倍，第 0 层是 1300 倍；只让最后一层保持 BF16，正确率也救不回来。</p>

<h2 id="修法">修法</h2>

<ol>
  <li><strong>跳过第 0 层</strong>（见上）。简单，现在就能用。</li>
  <li><strong>量化前减掉一个固定的 K 中心。</strong> 在 RoPE 之后给所有 key 减去同一个向量，同一个 query 的所有注意力分数只差一个常数，softmax 的结果不变。用在一段文本上测得的各通道 K 均值，R1-Distill-Qwen-7B 上的 FP8 回到 69.5%（离线模拟量化，提示部分用 BF16；同样设置下 BF16 是 73.5%）。</li>
  <li><strong>直接用 key 的偏置当这个中心。</strong> 阿里的 rtp-llm 已经这么做了（<a href="https://github.com/alibaba/rtp-llm/pull/1583">alibaba/rtp-llm#1583</a>）：不用标定，不加算子，只作用于带 QKV 偏置的层。vLLM 里有一个还没合并的 PR（<a href="https://github.com/vllm-project/vllm/pull/59674">#59674</a>）针对 INT4 cache 走了一条精确的路：缓存前去掉偏置，再在注意力分数里把它随位置变化的那部分加回来。</li>
</ol>

<h2 id="出处">出处</h2>

<p>这个机制不是我发现的。Liu 等人的《Quantization Hurts Reasoning?》（<a href="https://arxiv.org/abs/2504.04823">arXiv 2504.04823</a>）已经在 4 位 KV cache 上指出，R1-Distill-Qwen-1.5B / 7B 的 K 离群通道来自继承自 Qwen2.5 的 key 偏置，图里画的就是第 0 层。rtp-llm 在自己的引擎里修好了 FP8 的情况。vLLM 官方的 <a href="https://vllm.ai/blog/2026-04-22-fp8-kvcache">FP8 KV 文章</a>（2026 年 4 月）讲的是 Qwen3 / Qwen3.5 和长上下文的累加精度问题，没有涉及 Qwen2 系列。在 vLLM 内部，INT4 cache 上的同一个问题已经有人报告（#59671），内核修复在 <a href="https://github.com/vllm-project/vllm/pull/59674">#59674</a>，启动时的警告在 <a href="https://github.com/vllm-project/vllm/pull/63081">#63081</a>；#63081 还测到默认缩放因子的 FP8 cache 让 Qwen2.5-7B 和 1.5B 在检索任务上崩溃，并给出 <code class="language-plaintext highlighter-rouge">--kv-cache-dtype-skip-layers 0</code> 这个修法。我补充的是推理基准上的实测（MATH-500，R1-Distill 系列）、Llama 底座的对照，以及第 0 层和最后一层的对比。</p>

<p>讨论在 <a href="https://github.com/vllm-project/vllm/issues/41343">vLLM #41343</a>。如果你在别的模型上也遇到过，欢迎告诉我。</p>

<p><em>更正（2026 年 11 月 16 日）：本文第一版里 R1-Distill-Llama-8B 的数字，是在分词器加载出错（<a href="https://github.com/huggingface/transformers/issues/45488">transformers #45488</a>）的情况下测的：vLLM 里的提示丢了空格和换行。Llama 那一行、图和正文的数字已换成修好分词器后重跑的结果（同样设置，全部 500 题，跑一次）：BF16 68.4%，FP8 67.0%，FP8 且第 0 层用 BF16 68.8%。结论不变：这个模型没有 key 偏置，FP8 下不崩。Qwen 的数字不受影响。</em></p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="kv-cache" /><category term="quantization" /><category term="vllm" /><summary type="html"><![CDATA[vLLM 0.33 默认的 FP8 KV cache 让 R1-Distill-Qwen-7B 在 MATH-500 上从 71% 掉到 0.5%，不报错。原因在第 0 层的 key 偏置，第 0 层保持 BF16 就能修好。]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://thomaszhang2661.github.io/assets/fp8_qwen2_zh.png" /><media:content medium="image" url="https://thomaszhang2661.github.io/assets/fp8_qwen2_zh.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">FP8 KV cache can silently break small Qwen2-based reasoning models in vLLM. Here’s the one-line check.</title><link href="https://thomaszhang2661.github.io/2026/11/13/fp8-kv-cache-qwen2-vllm/" rel="alternate" type="text/html" title="FP8 KV cache can silently break small Qwen2-based reasoning models in vLLM. Here’s the one-line check." /><published>2026-11-13T00:00:00+00:00</published><updated>2026-11-13T00:00:00+00:00</updated><id>https://thomaszhang2661.github.io/2026/11/13/fp8-kv-cache-qwen2-vllm</id><content type="html" xml:base="https://thomaszhang2661.github.io/2026/11/13/fp8-kv-cache-qwen2-vllm/"><![CDATA[<p><a href="/2026/11/13/fp8-kv-cache-qwen2-vllm-zh/">中文版</a></p>

<p><strong>TL;DR</strong></p>
<ul>
  <li>With vLLM’s default FP8 KV cache, Qwen2-based models at 7B and below fall apart on long reasoning:
R1-Distill-Qwen-7B goes from 71% to 0.5% on MATH-500, with no error or warning.</li>
  <li>The same R1 distillation on a Llama base is fine (67.0% vs 68.4%). The 14B Qwen model is fine too.</li>
  <li>The cause is layer 0: Qwen2’s key bias makes its keys almost all mean, and FP8’s rounding error lands on the small
part attention uses. Check and fix with one line: <code class="language-plaintext highlighter-rouge">kv_cache_dtype_skip_layers=["0"]</code>.</li>
</ul>

<p>Turn on <code class="language-plaintext highlighter-rouge">kv_cache_dtype="fp8"</code> in vLLM, run DeepSeek-R1-Distill-Qwen-7B on a math benchmark, and accuracy drops from
71% to 0.5%. No error, no warning. The model just starts repeating itself:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>To convert convert convert the point point point ...
</code></pre></div></div>

<p>Here is what I measured, why it happens, and how to fix it.</p>

<h2 id="the-numbers">The numbers</h2>

<p>vLLM 0.33.0, H200, MATH-500 (first 200 problems), greedy decoding, up to 4,096 new tokens, FP8 (E4M3) KV cache with
vLLM’s default (uncalibrated) scales.</p>

<table>
  <thead>
    <tr>
      <th>Model</th>
      <th>BF16 KV</th>
      <th>FP8 KV</th>
      <th>FP8, layer 0 kept in BF16</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>DeepSeek-R1-Distill-Qwen-7B</td>
      <td>71.0%</td>
      <td>0.5%</td>
      <td>71.5%</td>
    </tr>
    <tr>
      <td>Qwen2.5-7B-Instruct</td>
      <td>73.5%</td>
      <td>1.5%</td>
      <td>72.0%</td>
    </tr>
    <tr>
      <td>DeepSeek-R1-Distill-Qwen-1.5B</td>
      <td>61.0%</td>
      <td>0.0%</td>
      <td>61.0%</td>
    </tr>
    <tr>
      <td>DeepSeek-R1-Distill-Qwen-14B</td>
      <td>70.0%</td>
      <td>73.0%</td>
      <td>–</td>
    </tr>
    <tr>
      <td>DeepSeek-R1-Distill-Llama-8B (all 500 problems)</td>
      <td>68.4%</td>
      <td>67.0%</td>
      <td>68.8%</td>
    </tr>
  </tbody>
</table>

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 740 460" width="100%" role="img" aria-label="MATH-500 accuracy in vLLM: FP8 KV cache vs BF16" style="font-family:-apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Noto Sans SC', Helvetica, Arial, sans-serif;max-width:740px">
<rect width="740" height="460" fill="#fcfcfb" />
<text x="64" y="34" font-size="20" font-weight="600" fill="#0b0b0b">MATH-500 accuracy in vLLM: FP8 KV cache vs BF16</text>
<text x="64" y="58" font-size="13" fill="#52514e">vLLM 0.33.0, H200, greedy, 4,096 tokens, default FP8 scales</text>
<rect x="64" y="76" width="12" height="12" rx="3" fill="#2a78d6" />
<text x="82" y="86.5" font-size="13" fill="#0b0b0b">BF16 KV</text>
<rect x="165.39999999999998" y="76" width="12" height="12" rx="3" fill="#eb6834" />
<text x="183.39999999999998" y="86.5" font-size="13" fill="#0b0b0b">FP8 KV</text>
<rect x="258.59999999999997" y="76" width="12" height="12" rx="3" fill="#1baf7a" />
<text x="276.59999999999997" y="86.5" font-size="13" fill="#0b0b0b">FP8, layer 0 in BF16</text>
<line x1="64" x2="716" y1="368.0" y2="368.0" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="372.0" font-size="12" fill="#7a7974" text-anchor="end">0%</text>
<line x1="64" x2="716" y1="305.5" y2="305.5" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="309.5" font-size="12" fill="#7a7974" text-anchor="end">20%</text>
<line x1="64" x2="716" y1="243.0" y2="243.0" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="247.0" font-size="12" fill="#7a7974" text-anchor="end">40%</text>
<line x1="64" x2="716" y1="180.5" y2="180.5" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="184.5" font-size="12" fill="#7a7974" text-anchor="end">60%</text>
<line x1="64" x2="716" y1="118.0" y2="118.0" stroke="#e4e3df" stroke-width="1" />
<text x="54" y="122.0" font-size="12" fill="#7a7974" text-anchor="end">80%</text>
<path d="M88.2,368.0 V150.1 Q88.2,146.1 92.2,146.1 H110.2 Q114.2,146.1 114.2,150.1 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Qwen-7B · BF16 KV: 71.0%</title></path>
<path d="M116.2,368.0 V367.2 Q116.2,366.4 117.0,366.4 H141.4 Q142.2,366.4 142.2,367.2 V368.0 Z" fill="#eb6834"><title>R1-Distill-Qwen-7B · FP8 KV: 0.5%</title></path>
<text x="129.2" y="359.4" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">0.5%</text>
<path d="M144.2,368.0 V148.6 Q144.2,144.6 148.2,144.6 H166.2 Q170.2,144.6 170.2,148.6 V368.0 Z" fill="#1baf7a"><title>R1-Distill-Qwen-7B · FP8, layer 0 in BF16: 71.5%</title></path>
<text x="129.2" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Qwen-7B</text>
<path d="M218.6,368.0 V142.3 Q218.6,138.3 222.6,138.3 H240.6 Q244.6,138.3 244.6,142.3 V368.0 Z" fill="#2a78d6"><title>Qwen2.5-7B-Instruct · BF16 KV: 73.5%</title></path>
<path d="M246.6,368.0 V365.7 Q246.6,363.3 248.9,363.3 H270.3 Q272.6,363.3 272.6,365.7 V368.0 Z" fill="#eb6834"><title>Qwen2.5-7B-Instruct · FP8 KV: 1.5%</title></path>
<text x="259.6" y="356.3" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">1.5%</text>
<path d="M274.6,368.0 V147.0 Q274.6,143.0 278.6,143.0 H296.6 Q300.6,143.0 300.6,147.0 V368.0 Z" fill="#1baf7a"><title>Qwen2.5-7B-Instruct · FP8, layer 0 in BF16: 72.0%</title></path>
<text x="259.6" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">Qwen2.5-7B-Instruct</text>
<path d="M349.0,368.0 V181.4 Q349.0,177.4 353.0,177.4 H371.0 Q375.0,177.4 375.0,181.4 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Qwen-1.5B · BF16 KV: 61.0%</title></path>
<rect x="377.0" y="367.0" width="26" height="1" fill="#eb6834"><title>R1-Distill-Qwen-1.5B · FP8 KV: 0.0%</title></rect>
<text x="390.0" y="361.0" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">0.0%</text>
<path d="M405.0,368.0 V181.4 Q405.0,177.4 409.0,177.4 H427.0 Q431.0,177.4 431.0,181.4 V368.0 Z" fill="#1baf7a"><title>R1-Distill-Qwen-1.5B · FP8, layer 0 in BF16: 61.0%</title></path>
<text x="390.0" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Qwen-1.5B</text>
<path d="M479.4,368.0 V153.2 Q479.4,149.2 483.4,149.2 H501.4 Q505.4,149.2 505.4,153.2 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Qwen-14B · BF16 KV: 70.0%</title></path>
<path d="M507.4,368.0 V143.9 Q507.4,139.9 511.4,139.9 H529.4 Q533.4,139.9 533.4,143.9 V368.0 Z" fill="#eb6834"><title>R1-Distill-Qwen-14B · FP8 KV: 73.0%</title></path>
<text x="520.4" y="132.9" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">73.0%</text>
<text x="548.4" y="362.0" font-size="11" fill="#7a7974" text-anchor="middle">n/a</text>
<text x="520.4" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Qwen-14B</text>
<path d="M609.8,368.0 V158.2 Q609.8,154.2 613.8,154.2 H631.8 Q635.8,154.2 635.8,158.2 V368.0 Z" fill="#2a78d6"><title>R1-Distill-Llama-8B · BF16 KV: 68.4%</title></path>
<path d="M637.8,368.0 V162.6 Q637.8,158.6 641.8,158.6 H659.8 Q663.8,158.6 663.8,162.6 V368.0 Z" fill="#eb6834"><title>R1-Distill-Llama-8B · FP8 KV: 67.0%</title></path>
<text x="650.8" y="151.6" font-size="10.5" font-weight="600" fill="#0b0b0b" text-anchor="middle">67.0%</text>
<path d="M665.8,368.0 V157.0 Q665.8,153.0 669.8,153.0 H687.8 Q691.8,153.0 691.8,157.0 V368.0 Z" fill="#1baf7a"><title>R1-Distill-Llama-8B · FP8, layer 0 in BF16: 68.8%</title></path>
<text x="650.8" y="388.0" font-size="11.5" fill="#0b0b0b" text-anchor="middle">R1-Distill-Llama-8B</text>
<path d="M74.0,402.0 v5 H575.6 v-5" fill="none" stroke="#7a7974" stroke-width="1" />
<text x="324.8" y="422.0" font-size="12" fill="#52514e" text-anchor="middle">Qwen2 base</text>
<path d="M595.6,402.0 v5 H706.0 v-5" fill="none" stroke="#7a7974" stroke-width="1" />
<text x="650.8" y="422.0" font-size="12" fill="#52514e" text-anchor="middle">Llama base</text>
<text x="64" y="450" font-size="11.5" fill="#7a7974">First 200 problems; R1-Distill-Llama-8B: all 500. Full numbers in the table below.</text>
</svg>

<p>Keeping only the <strong>last</strong> layer in BF16 instead does nothing (R1-Distill-Qwen-7B: 0.0% on 40 problems). The 14B model
is fine, and so are Qwen2.5-14B and Llama-3.1-8B in my offline perplexity runs.</p>

<p>The cleanest control is DeepSeek-R1-Distill-Llama-8B: the same R1 distillation, on a Llama-3.1 base, which has no key
bias. FP8 on every layer costs it 1.4 points (paired permutation test p = 0.47, one run over all 500 problems). The
collapse comes with the Qwen2 base, not with reasoning or with FP8 as such.</p>

<h2 id="check-if-youre-hit">Check if you’re hit</h2>

<p>Run the same prompt with layer 0 excluded from FP8:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">llm</span> <span class="o">=</span> <span class="n">LLM</span><span class="p">(</span>
    <span class="n">model</span><span class="o">=</span><span class="s">"deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"</span><span class="p">,</span>
    <span class="n">kv_cache_dtype</span><span class="o">=</span><span class="s">"fp8"</span><span class="p">,</span>
    <span class="n">kv_cache_dtype_skip_layers</span><span class="o">=</span><span class="p">[</span><span class="s">"0"</span><span class="p">],</span>
<span class="p">)</span>
</code></pre></div></div>

<p>If the output goes back to normal, this is your problem. It costs one layer out of 28 in BF16.</p>

<p>One caveat: with the FlashAttention-3 backend this currently crashes on some models
(<a href="https://github.com/vllm-project/vllm/issues/65821">vLLM #65821</a>; a fix is open in
<a href="https://github.com/vllm-project/vllm/pull/65891">#65891</a>). Adding <code class="language-plaintext highlighter-rouge">attention_backend="FLASHINFER"</code> works around it.</p>

<h2 id="why-layer-0">Why layer 0</h2>

<p>Qwen2’s key projection has a bias. In layer 0 that bias dominates: the norm of the per-channel K mean is 98% of the
norm of K (Llama-3.1-8B: 68%).</p>

<p>Attention only cares about the part of K that differs between tokens. The mean adds the same number to every attention
score of a given query, and softmax cancels it. But FP8’s rounding error is relative to the whole value, so it lands on
the small part that actually matters. Think of measuring height differences between people standing on a mountain
with a ruler marked every hundred meters.</p>

<p>A per-tensor scale can keep large values from saturating, but it does not change E4M3’s relative precision, so it
cannot remove the rounding error on that small token-dependent part. And in layer 0 nothing saturates anyway: on
wikitext its largest key value is 203 (R1-Distill-Qwen-7B) and 172 (Qwen2.5-7B-Instruct), well below E4M3’s maximum
of 448. (All accuracy numbers here use vLLM’s default scales; I have not run calibrated scales in vLLM.)</p>

<p>And layer 0’s errors don’t stay in layer 0. Quantizing only layer 0 to FP8 gives 1,300× the output KL of the median
layer (offline, R1-Distill-Qwen-7B, wikitext). In a long chain of thought that compounds into nonsense. Offline,
Qwen2.5-7B’s wikitext perplexity goes from 6.31 to 78.2 with FP8 on every layer, and to 6.59 with layer 0 kept.</p>

<h3 id="but-nvidia-says-its-the-last-layer">“But NVIDIA says it’s the last layer”</h3>

<p>NVIDIA’s <a href="https://nvidia.github.io/TensorRT-Edge-LLM/latest/user_guide/features/FP8KV.html">TensorRT-Edge-LLM docs</a>
flag Qwen2.5-7B-Instruct for FP8 KV degradation and point at the last layer’s very large values. Both observations
hold: the last layer’s K has a mean almost as large as layer 0’s (mean norm 240 vs 274; middle layers ~12). But large
values are not the same as sensitivity. Quantizing only the last layer gives 8× the median layer’s KL, against 1,300×
for layer 0, and keeping only the last layer in BF16 does not recover accuracy.</p>

<h2 id="fixes">Fixes</h2>

<ol>
  <li><strong>Skip layer 0</strong> (above). Simple, works today.</li>
  <li><strong>Subtract a fixed K center before quantizing.</strong> Shifting every key by the same vector after RoPE changes all
attention scores of a query by one constant, so the softmax output is unchanged. With a per-channel K mean measured
on some text, FP8 recovers to 69.5% on R1-Distill-Qwen-7B (offline, fake-quantized FP8 with the prompt in BF16;
BF16 73.5% in that setup).</li>
  <li><strong>Use the K bias itself as that center.</strong> <a href="https://github.com/alibaba/rtp-llm/pull/1583">alibaba/rtp-llm#1583</a>
does this for FP8: no calibration, no extra kernel, only for layers with a QKV bias. In vLLM, an open PR
(<a href="https://github.com/vllm-project/vllm/pull/59674">#59674</a>) takes an exact route for the INT4 cache: it removes the
bias before caching and adds its position-dependent contribution back into the attention scores.</li>
</ol>

<h2 id="credit">Credit</h2>

<p>The mechanism isn’t new. Liu et al., <em>Quantization Hurts Reasoning?</em>
(<a href="https://arxiv.org/abs/2504.04823">arXiv 2504.04823</a>), traced the extreme K outlier channels in
R1-Distill-Qwen-1.5B / 7B to the key bias inherited from Qwen2.5, shown for layer 0, for 4-bit KV caches. rtp-llm
fixed the FP8 case in its engine. vLLM’s own <a href="https://vllm.ai/blog/2026-04-22-fp8-kvcache">FP8 KV write-up</a>
(April 2026) covers Qwen3 / Qwen3.5 and the long-context accumulation issue, not the Qwen2 family. Inside vLLM, the
same failure was reported for the INT4 cache (#59671), with a kernel fix proposed in
<a href="https://github.com/vllm-project/vllm/pull/59674">#59674</a> and a startup warning in
<a href="https://github.com/vllm-project/vllm/pull/63081">#63081</a>. #63081 also shows the FP8 cache at default scales
collapsing Qwen2.5-7B and 1.5B on a retrieval task, and names <code class="language-plaintext highlighter-rouge">--kv-cache-dtype-skip-layers 0</code> as the remedy. What I
add is the picture on a reasoning benchmark (MATH-500, R1-Distill models), the Llama-base control, and the layer-0 vs
last-layer measurement.</p>

<p>Discussion is on <a href="https://github.com/vllm-project/vllm/issues/41343">vLLM #41343</a>. If you’ve seen this on other
models, I’d like to hear about it.</p>

<p><em>Correction (Nov 16, 2026): The first version of this post used R1-Distill-Llama-8B numbers measured with a tokenizer-loading bug (<a href="https://github.com/huggingface/transformers/issues/45488">transformers #45488</a>) that removed spaces and newlines from the prompts in vLLM. The Llama row, the chart and the numbers in the text now come from a rerun with the correct tokenizer (same setup, all 500 problems, one run): BF16 68.4%, FP8 67.0%, FP8 with layer 0 in BF16 68.8%. The conclusion is unchanged: this model has no key bias and does not collapse under FP8. The Qwen numbers were not affected.</em></p>]]></content><author><name>Jian (Thomas) Zhang</name></author><category term="kv-cache" /><category term="quantization" /><category term="vllm" /><summary type="html"><![CDATA[In vLLM 0.33 the default FP8 KV cache drops R1-Distill-Qwen-7B from 71% to 0.5% on MATH-500, with no error. The cause is layer 0's key bias; keeping layer 0 in BF16 fixes it.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://thomaszhang2661.github.io/assets/fp8_qwen2_en.png" /><media:content medium="image" url="https://thomaszhang2661.github.io/assets/fp8_qwen2_en.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>