<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>GPUMemory on MakiNaruto's Blog</title><link>https://makinaruto.github.io/tags/gpumemory/</link><description>Recent content in GPUMemory on MakiNaruto's Blog</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Wed, 15 Jan 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://makinaruto.github.io/tags/gpumemory/index.xml" rel="self" type="application/rss+xml"/><item><title>LLM - GPU显存占用</title><link>https://makinaruto.github.io/posts/nlp/llm_learning/gpumemory/</link><pubDate>Wed, 15 Jan 2025 00:00:00 +0000</pubDate><guid>https://makinaruto.github.io/posts/nlp/llm_learning/gpumemory/</guid><description>&lt;h2 id="存储分类">存储分类&lt;a class="anchorjs-link" href="#%e5%ad%98%e5%82%a8%e5%88%86%e7%b1%bb">&lt;/a>&lt;/h2>&lt;p>首先先看看模型计算过程中, 哪些过程需要被存储下来.&lt;br>
存储主要分为两大块：静态显存 + 动态显存&lt;br>
M_total = 模型参数 + 梯度 + 优化器状态 + Activation&lt;br>&lt;/p>
&lt;p>Model States 指和模型本身息息相关的，必须存储的内容，具体包括：&lt;/p>
&lt;ul>
&lt;li>parameters(固定)：模型参数&lt;/li>
&lt;li>gradients(固定)：梯度&lt;/li>
&lt;li>optimizer states(固定)：优化器状态，例如 Adam 优化器的 momentum 和 variance 等。&lt;/li>
&lt;li>activation(固定)：激活值。虽然它不是必须存储的，但在训练过程中会额外产生的内容.&lt;/li>
&lt;/ul>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>Model States&lt;/th>
 &lt;th>精度&lt;/th>
 &lt;th>Bytes&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>Model Weights&lt;/td>
 &lt;td>FP16&lt;/td>
 &lt;td>2&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Gradients&lt;/td>
 &lt;td>FP16&lt;/td>
 &lt;td>2&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Master Weights&lt;/td>
 &lt;td>FP32&lt;/td>
 &lt;td>4&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Adam Optimizer (m/v)&lt;/td>
 &lt;td>FP32&lt;/td>
 &lt;td>8&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Total / 总计&lt;/td>
 &lt;td>-&lt;/td>
 &lt;td>16 Bytes&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;h3 id="activation">Activation&lt;a class="anchorjs-link" href="#activation">&lt;/a>&lt;/h3>&lt;p>M_act ≈ C × L × B × S × H × bytes&lt;/p>
&lt;p>📊 显存优化效果对比&lt;br>
无 Checkpoint : C≈10~15 (显存占用极高)&lt;br>
开启 Checkpoint : C≈2~4 (显存降低3~5倍)&lt;br>
FlashAttention : C≈1.5 (显存占用最低)&lt;br>
Checkpoint的核心策略, &lt;strong>用计算换取显存&lt;/strong>, 反向传播时重算部分激活值，大幅降低峰值.&lt;br>&lt;/p></description></item></channel></rss>