Buckets:

mishig's picture
download
raw
29.4 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;FSDP2&quot;,&quot;local&quot;:&quot;fsdp2&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;分片策略&quot;,&quot;local&quot;:&quot;sharding-strategies&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;配置 FSDP&quot;,&quot;local&quot;:&quot;configure-fsdp&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;使用上下文并行进行评估&quot;,&quot;local&quot;:&quot;evaluate-with-context-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;后续步骤&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DSYYoqAA.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BovLmb3z.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/WyYpyc-L.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DYBfcveH.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/0.ByO_lNbj.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/ZtBhN-Ei.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/57.DzD9ySQM.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BDdFI6Uf.js" rel="modulepreload">
<!--xe62cc--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;FSDP2&quot;,&quot;local&quot;:&quot;fsdp2&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Sharding strategies&quot;,&quot;local&quot;:&quot;sharding-strategies&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Configure FSDP&quot;,&quot;local&quot;:&quot;configure-fsdp&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Evaluate with context parallelism&quot;,&quot;local&quot;:&quot;evaluate-with-context-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="fsdp2" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#fsdp2"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->FSDP2<!----></span></h1><!--]--><!----> <p><a href="https://docs.pytorch.org/tutorials/intermediate/FSDP_tutorial.html" rel="nofollow">Fully Sharded Data Parallel (FSDP2)</a> 将模型、梯度和优化器状态分片到多个 GPU 上。在计算之前,每个 GPU 会从所有分片中收集完整的参数集合,计算完成后释放这些参数。分片机制允许你训练超出单个 GPU 显存容量的模型,但代价是比 <a href="./ddp">DDP</a> 产生更多的通信开销。当你的模型或优化器状态无法放入单个 GPU 时,请使用 FSDP。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-text "><!----> ┌─────────────────┐
│ training data │
└────────┬────────┘
┌──────────────────┼──────────────────┐
│ shard 0 │ shard 1 │ shard 2
▼ ▼ ▼
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ param │ │ param │ │ param │
│ shard 0 │ │ shard 1 │ │ shard 2 │
│ GPU 0 │ │ GPU 1 │ │ GPU 2 │
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘
│ │ │
└──────── all-gather (params) ────────┘
│
full params on each GPU
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
forward forward forward
│ │ │
└───── reduce-scatter (grads) ────────┘
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
grad shard 0 grad shard 1 grad shard 2
optim shard 0 optim shard 1 optim shard 2
step step step<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="sharding-strategies" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sharding-strategies"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->分片策略<!----></span></h2><!--]--><!----> <p>FSDP2 通过 <code>~TrainingArguments.fsdp_config</code> 控制分片。设置 <code>fsdp=True</code> 以启用 FSDP,并在 FSDP 配置中设置 <code>reshard_after_forward</code> 来选择内存与吞吐量之间的权衡。</p> <table><thead><tr><th><code>reshard_after_forward</code></th><th>行为</th></tr></thead><tbody><tr><td><code>true</code></td><td>在前向传播后重新分片参数以节省更多内存</td></tr><tr><td><code>false</code></td><td>在正向传播和反向传播之间保持参数聚合,以避免重新执行 all-gather,代价是更高的峰值内存</td></tr></tbody></table> <p><code>auto_wrap_policy</code> 控制模块如何被封装为 FSDP 单元。其默认值为 <code>"TRANSFORMER_BASED_WRAP"</code>,该策略会将模型的 transformer 层进行封装。如果不进行封装(<code>"NO_WRAP"</code>),整个模型将作为一个 FSDP 单元,从而失去分片带来的内存优势。</p> <!--[1--><h2 class="relative group"><a id="configure-fsdp" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#configure-fsdp"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->配置 FSDP<!----></span></h2><!--]--><!----> <p>这些字段控制 FSDP2 如何包装、分片和加载模型。<code>reshard_after_forward</code> 和 <code>auto_wrap_policy</code> 在 <a href="#sharding-strategies">分片策略</a> 中进行了介绍。</p> <ul><li><p><code>cpu_offload</code> 会在参数和梯度未被使用时将其卸载到 CPU,以节省 GPU 内存。</p></li> <li><p><code>transformer_layer_cls_to_wrap</code> 定义了当 <code>auto_wrap_policy</code> 为 <code>"TRANSFORMER_BASED_WRAP"</code> 时,需要封装到 FSDP 单元中的 transformer 层。每个单元独立管理自身的 gather 和 scatter 操作。在前向传播过程中,仅收集当前单元的参数,而释放之前单元的参数以节省内存。</p> <p>仅包装顶层模型不会带来任何 GPU 显存节省。包装每一个单独的 <code>Linear</code> 层会使单元间通信变得非常昂贵。将此字段留空,FSDP 将从模型定义中读取该值。</p></li> <li><p><code>min_num_params</code> 设置了基于大小进行包装时每个模块的最小参数量。它仅在 <code>auto_wrap_policy</code> 为 <code>"SIZE_BASED_WRAP"</code> 时使用。</p></li> <li><p><code>state_dict_type</code> 控制检查点的格式。默认值为 <code>"FULL_STATE_DICT"</code>,对应单个兼容 Transformers 的检查点。对于大型模型,使用 <code>"SHARDED_STATE_DICT"</code> 可以为每个 rank 生成一个检查点文件,速度更快。分片检查点只能加载回 FSDP,因此对于最终需要共享或在 FSDP 之外加载的检查点,请保存为 <code>"FULL_STATE_DICT"</code>。</p></li> <li><p><code>cpu_ram_efficient_loading</code> 仅在 rank 0 上从磁盘加载检查点。其他 GPU 初始化一个空模型,并通过广播接收权重,从而避免多个进程将大型模型加载到 CPU 内存中。</p></li> <li><p><code>activation_checkpointing</code> 会在反向传播过程中重新计算激活值,而不是将其存储。请在 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.TrainingArguments">TrainingArguments</a> 中使用此参数来替代 <a href="./grad_checkpointing">gradient checkpointing</a>。同时设置两者会引发错误。</p></li></ul> <p>通过 <a href="./accelerate#accelerate-config-file">Accelerate 配置文件</a> 或传递给 <code>fsdp_config</code> 的 FSDP 配置文件来配置 FSDP 训练。</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">Accelerate 配置文件</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">FSDP 配置文件</div><!--]--></div> <div class="language-select"><div><p>运行 <a href="https://huggingface.co/docs/accelerate/en/package_reference/cli#accelerate-config" rel="nofollow">accelerate config</a> 命令,并根据你的硬件和训练配置回答相关问题。这会在你的缓存中创建一个 <code>default_config.yaml</code> 文件。</p> <p>使用 <a href="https://huggingface.co/docs/accelerate/en/package_reference/cli#accelerate-launch" rel="nofollow">accelerate launch</a> 运行基于 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer">Trainer</a> 的脚本。<code>fsdp_config</code> 是多余的,因为 Accelerate 配置文件已涵盖相同的设置。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-cli "><!---->accelerate <span class="hljs-built_in">launch</span> train.py<!----></pre></div><!----><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;version&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">2</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;reshard_after_forward&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;cpu_offload&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">false</span></span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;auto_wrap_policy&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;TRANSFORMER_BASED_WRAP&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;transformer_layer_cls_to_wrap&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-string">&quot;LlamaDecoderLayer&quot;</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;state_dict_type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;FULL_STATE_DICT&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;cpu_ram_efficient_loading&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;activation_checkpointing&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-literal"><span class="hljs-keyword">true</span></span>
<span class="hljs-punctuation">}</span><!----></pre></div><!----> <p>设置 <code>fsdp=True</code>,并将 FSDP 配置文件传递给 <code>fsdp_config</code>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TrainingArguments
TrainingArguments(
...,
fsdp=<span class="hljs-literal">True</span>,
fsdp_config=<span class="hljs-string">&quot;path/to/fsdp.json&quot;</span>,
)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <blockquote class="tip"><p>对于混合专家模型,<code>fsdp_size</code> 可以与 <code>tp_size</code> 和 <code>ep_size</code> 结合使用,将专家分片到一个网格维度上,其余部分分片到另一个维度上。参见 <a href="./expert_parallelism#combining-with-fsdp2">专家并行</a>。</p></blockquote> <!--[1--><h2 class="relative group"><a id="evaluate-with-context-parallelism" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#evaluate-with-context-parallelism"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->使用上下文并行进行评估<!----></span></h2><!--]--><!----> <p>通过 Accelerate 的 <code>ParallelismConfig</code> 中的 <code>cp_size</code> 来设置 FSDP2 的上下文并行(CP)。启动时,需同时传入 <code>--use_parallelism_config</code> 和 <code>--parallelism_config_cp_size</code>。如果 Accelerate YAML 中的 <code>parallelism_config</code> 非空,也会自动启用 <code>use_parallelism_config</code>。如果仅传入 <code>--parallelism_config_cp_size</code>,该参数将被忽略,<code>cp_size=1</code> 保持不变。</p> <p>在 FSDP2 下,CP 将序列分片到各个 rank,使每个 GPU 持有上下文的一个切片(<code>allgather</code> 或 <code>alltoall</code>)。这与 <a href="./deepspeed_alst">Ulysses 序列并行</a> 不同,后者通过 DeepSpeed ALST 运行,并在同一配置中使用 <code>sp_size</code>。请在 CP 和 Ulysses SP 之间二选一。不要同时将 <code>cp_size</code> 和 <code>sp_size</code> 设置为大于 1。</p> <p><a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer.evaluate">evaluate()</a> 通过 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer.prediction_step">prediction_step()</a> 使用与 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer.train">train()</a> 相同的上下文并行上下文。在该上下文激活期间,每个 rank 运行其序列分片。<code>eval_loss</code> 是正确的损失值(未乘以 <code>cp_size</code>)。早停和 <code>load_best_model_at_end</code> 使用该指标。</p> <p>Logits 沿序列维度保持分片状态,且没有任何机制将其重组为完整长度的预测。在 CP 下,不支持基于 logits 的预测以及 <code>compute_metrics</code>。</p> <p>当您需要 <code>eval_loss</code> 时,请复用现有的 FSDP2 + <code>cp_size</code> 启动配置。</p> <p>如果您子类化了 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer.prediction_step">prediction_step()</a> 并自行统计 token 数量,请在进入上下文并行上下文之前统计 <code>num_items_in_batch</code>。在该上下文激活期间,序列缓冲区会在各 rank 之间就地分片。之后再进行统计会导致批次数量被低估。默认的 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer">Trainer</a> 已经会先进行统计,只有当您重写 <code>prediction_step</code> 时才需要这样做。</p> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->后续步骤<!----></span></h2><!--]--><!----> <ul><li>如果模型可以放入单个 GPU,请参阅 <a href="./ddp">DDP</a> 以进行数据并行训练。</li> <li>有关 ZeRO 优化和 NVMe 卸载,请参阅 <a href="./deepspeed">DeepSpeed</a>。</li> <li>对于使用 PyTorch/XLA 在 TPU 上运行 FSDP,请在 <code>~TrainingArguments.fsdp_config</code> 中设置 <code>xla</code>、<code>xla_fsdp_settings</code> 和 <code>xla_fsdp_grad_ckpt</code>。</li> <li>Read the <a href="https://nanotron-ultrascale-playbook.static.hf.space/index.html#zero-3:_adding_parameter_partitioning_(fsdp)" rel="nofollow">FSDP chapter</a> from The Ultra-Scale Playbook for more information about how FSDP works.</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/fsdp.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_1m6yd5j = {
base: "/docs/transformers/main/en",
assets: "/docs/transformers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 57],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
29.4 kB
·
Xet hash:
b7c0ef59de1ea326816a8cd21e2ccaa7de0cf2b2cfd2c27168df049030469b8b

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.