Buckets:

hf-doc-build/doc / transformers /main /zh /expert_parallelism.html
mishig's picture
download
raw
77.9 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;专家并行&quot;,&quot;local&quot;:&quot;expert-parallelism&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DistributedConfig&quot;,&quot;local&quot;:&quot;distributedconfig&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;选择实现&quot;,&quot;local&quot;:&quot;choosing-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;添加实现&quot;,&quot;local&quot;:&quot;adding-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;覆盖计划&quot;,&quot;local&quot;:&quot;overriding-the-plans&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Token 分发&quot;,&quot;local&quot;:&quot;token-dispatch&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;尺寸如何组合&quot;,&quot;local&quot;:&quot;how-the-sizes-combine&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;使用张量并行&quot;,&quot;local&quot;:&quot;with-tensor-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;与 FSDP2 结合&quot;,&quot;local&quot;:&quot;combining-with-fsdp2&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;API 参考&quot;,&quot;local&quot;:&quot;transformers.DistributedConfig&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DS27pc2p.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Bbt6a5UJ.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DGxd7hEC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/hD794eHD.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DG_DvbXP.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.Dperbprz.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DIDdACb8.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/5.jXI389aE.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CBFQlbiL.js" rel="modulepreload">
<!--9m8k7m--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Expert parallelism&quot;,&quot;local&quot;:&quot;expert-parallelism&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DistributedConfig&quot;,&quot;local&quot;:&quot;distributedconfig&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Choosing an implementation&quot;,&quot;local&quot;:&quot;choosing-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Adding an implementation&quot;,&quot;local&quot;:&quot;adding-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Overriding the plans&quot;,&quot;local&quot;:&quot;overriding-the-plans&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Token dispatch&quot;,&quot;local&quot;:&quot;token-dispatch&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;How the sizes combine&quot;,&quot;local&quot;:&quot;how-the-sizes-combine&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;With tensor parallelism&quot;,&quot;local&quot;:&quot;with-tensor-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Combining with FSDP2&quot;,&quot;local&quot;:&quot;combining-with-fsdp2&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;API reference&quot;,&quot;local&quot;:&quot;transformers.DistributedConfig&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="expert-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="#expert-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></h1><!--]--><!----> <p><a href="https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=expert_parallelism" rel="nofollow">专家并行</a>是一种针对<a href="https://huggingface.co/blog/moe" rel="nofollow">混合专家(MoE)模型</a>的并行策略。每个专家的 feedforward 层位于不同的硬件加速器上。路由器将 token 分派给相应的专家并收集结果。由于每个 token 仅激活少数几个专家,该方法可以在不增加计算成本的情况下,将模型扩展到远更大的参数量。</p> <!--[1--><h2 class="relative group"><a id="distributedconfig" 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="#distributedconfig"><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><!---->DistributedConfig<!----></span></h2><!--]--><!----> <p>使用 <a href="/docs/transformers/main/zh/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a> 类和 <code>ep_size</code> 参数来启用专家并行。MoE 模型通过 <code>"ep_dispatch_experts"</code> 对其专家进行分片。其实现方式决定了 token 如何到达专家。<a href="#token-dispatch">Token 分发</a> 允许 <code>ep_size</code> 与 <code>tp_size</code> 不同。部分 EP 实现要求 <code>ep_size=tp_size</code>,例如 <code>"all_reduce"</code> 和 <code>"deepgemm_megamoe"</code>,因此专家组中的每个 rank 都持有相同的 token。</p> <p>Llama 4 没有 EP 计划:其专家已经接收了由路由器分数缩放后的所有 token,因此无需进行分发。请改用张量并行(<code>tp_size</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">import</span> os
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer
<span class="hljs-keyword">from</span> transformers.distributed.configuration_utils <span class="hljs-keyword">import</span> DistributedConfig
distributed_config = DistributedConfig(
tp_size=<span class="hljs-built_in">int</span>(os.environ[<span class="hljs-string">&quot;WORLD_SIZE&quot;</span>]),
ep_size=<span class="hljs-built_in">int</span>(os.environ[<span class="hljs-string">&quot;WORLD_SIZE&quot;</span>]),
)
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;openai/gpt-oss-120b&quot;</span>,
distributed_config=distributed_config,
)<!----></pre></div><!----> <p>每个 MoE 模型在其配置中定义了两个计划:<code>base_model_tp_plan</code> 用于稠密模块,<code>base_model_ep_plan</code> 用于专家。它们在加载的模型上分别暴露为 <code>model.tp_plan</code> 和 <code>model.ep_plan</code>。当 <code>tp_size > 1</code> 且 <code>ep_size > 1</code> 时,两者同时生效:<a href="./perf_infer_gpu_multi">张量并行</a> 计划对注意力和稠密 MLP 进行分片,而专家并行计划对专家进行分片。对于相同的模块,EP 规则优先于 TP 规则,因此专家权重仅由 EP 计划分片一次。在 EP 计划中,<code>GroupedGemmParallel</code> 样式沿专家维度拆分专家权重,使每个 rank 仅加载其本地专家,而专家模块上的 <code>"ep_dispatch_experts"</code> 规则决定了 token 如何路由到这些专家。</p> <p><code>tp_plan</code> 仅在 <code>tp_size > 1</code> 时生效,<code>ep_plan</code> 仅在 <code>ep_size > 1</code> 时生效。当启用 TP 且禁用 EP 时,完整的 TP 计划将生效,包括专家规则。</p> <p><code>"ep_dispatch_experts"</code> 按 rank 对完整的专家进行分片(<code>ep_size</code> 是 <code>tp_size</code> 的倍数,且能整除 <code>fsdp_size * tp_size</code>),并通过 <code>ep_implementation</code> 指定的实现来访问它们,参见 <a href="#choosing-an-implementation">选择实现</a>。</p> <!--[2--><h3 class="relative group"><a id="choosing-an-implementation" 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="#choosing-an-implementation"><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></h3><!--]--><!----> <p><code>"ep_dispatch_experts"</code> 对专家进行一次性分片,并从在 <code>EPImplementationInterface</code> 中注册的实现中选择令牌到达专家的方式,该选择通过 <a href="/docs/transformers/main/zh/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a> 中的 <code>ep_implementation</code> 完成:</p> <table><thead><tr><th align="left"><code>ep_implementation</code></th><th align="left">优势</th><th align="left">缺点</th></tr></thead><tbody><tr><td align="left"><code>"all_to_all"</code>,参见 <a href="#token-dispatch">token 分发</a></td><td align="left">适用于任意布局,包括 <code>ep_size > tp_size</code>。只有路由的 token 会移动。</td><td align="left">每层包含两次 all-to-all 集合通信和一次主机同步,因此无法被 CUDA 图捕获。</td></tr><tr><td align="left"><code>"all_reduce"</code></td><td align="left">没有令牌移动,也没有主机同步,因此解码阶段可以捕获到 CUDA 图中。</td><td align="left">要求 <code>ep_size=tp_size</code>。每个 rank 都在整个 batch 上运行其专家,因此其开销会随 batch 增大而增加。</td></tr><tr><td align="left"><code>"deepgemm_megamoe"</code></td><td align="left">DeepGEMM Mega MoE 将 dispatch、experts 和 combine 融合到一个 kernel 中。EP 组中的每一层都共享其对称内存缓冲区。</td><td align="left">要求 <code>ep_size=tp_size</code>,并且在 SM100+ 上使用 FP8 激活值和 FP4 专家。仅支持推理。</td></tr><tr><td align="left"><code>"auto"</code>,默认值</td><td align="left">当专家组内的各个 rank 共享同一个 batch 时,在推理阶段执行 <code>"all_reduce"</code>;否则执行 <code>"all_to_all"</code>。</td><td align="left">并非总是最快的选择:工作负载可能从自定义实现中受益,请参阅<a href="#adding-an-implementation">添加实现</a>。</td></tr></tbody></table> <blockquote class="note"><p><code>ep_implementation</code> 仅在 <a href="/docs/transformers/main/zh/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a> 中设置的并行规模范围内进行选择。当专家组中的各个 rank 持有不同的批次时,例如 <code>tp_size=1</code> 且 <code>fsdp_size=ep_size</code>,<code>"all_to_all"</code> 是唯一适用的实现方式。</p></blockquote> <!--[2--><h3 class="relative group"><a id="adding-an-implementation" 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="#adding-an-implementation"><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></h3><!--]--><!----> <p>实现类继承自 <code>EPImplementation</code>,并注册在 <code>ep_implementation</code> 选择器下。它针对每个专家模块构建一次,因此可以维护通信缓冲区等状态。调用时传入该 rank 持有的批次、全局专家 ID,即可返回其每个 token 的加权专家输出。它使用 <code>experts_forward</code> 运行该 rank 的专家,输入为它带来的行、本地专家 ID 和权重。</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.distributed.expert_parallel <span class="hljs-keyword">import</span> EPImplementation, EPImplementationInterface
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyEP</span>(<span class="hljs-title class_ inherited__">EPImplementation</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__call__</span>(<span class="hljs-params">self, hidden_states, top_k_index, top_k_weights</span>):
tokens, expert_ids, weights = ... <span class="hljs-comment"># bring this rank&#x27;s experts the rows routed to them</span>
expert_output = <span class="hljs-variable language_">self</span>.experts_forward(tokens, expert_ids, weights)
<span class="hljs-keyword">return</span> ... <span class="hljs-comment"># the weighted expert output for each token of the batch this rank holds</span>
EPImplementationInterface.register(<span class="hljs-string">&quot;my_ep&quot;</span>, MyEP)
distributed_config = DistributedConfig(tp_size=<span class="hljs-number">1</span>, fsdp_size=<span class="hljs-number">8</span>, ep_size=<span class="hljs-number">8</span>, ep_implementation=<span class="hljs-string">&quot;my_ep&quot;</span>)<!----></pre></div><!----> <blockquote class="note"><p>梯度必须到达持有该 token 的每个 rank,包括路由权重,因此请将 autograd 不会对其求导的集合通信操作包裹在 <code>torch.autograd.Function</code> 中。</p></blockquote> <blockquote class="tip"><p><code>enable_expert_parallel=True</code> 在未指定 <code>ep_size</code> 的情况下已被弃用,并会发出 <code>FutureWarning</code>。它仅对专家进行分片,效果等同于 <code>ep_size=tp_size, tp_plan={}</code>。</p></blockquote> <p>使用 <a href="https://pytorch.org/docs/stable/elastic/run.html" rel="nofollow">torchrun</a> 启动您的推理脚本。进程数必须等于 <code>tp_size * fsdp_size * pp_size</code>,并且 <code>ep_size</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-zsh "><!---->torchrun --nproc-per-node 8 your_script.py<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="overriding-the-plans" 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="#overriding-the-plans"><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></h3><!--]--><!----> <p>将 <code>tp_plan={...}</code> 或 <code>ep_plan={...}</code> 传递给 <a href="/docs/transformers/main/zh/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a>,以覆盖预定义计划中的个别规则。未指定的规则将保留,合并后的计划会存储在模型上。每个键必须与某个模块、参数或现有计划条目相匹配;否则,在分片之前加载时就会引发 <code>ValueError</code>。请使用从已加载模型视角看到的完整路径,例如对于因果语言模型使用 <code>model.layers.*</code>,对于其基础模型使用 <code>layers.*</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 "><!---->distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">4</span>,
ep_size=<span class="hljs-number">4</span>,
tp_plan={<span class="hljs-string">&quot;model.layers.*.self_attn.q_proj&quot;</span>: <span class="hljs-string">&quot;colwise_rep&quot;</span>},
ep_plan={<span class="hljs-string">&quot;model.layers.*.mlp.experts.down_proj&quot;</span>: <span class="hljs-string">&quot;grouped_gemm&quot;</span>},
)<!----></pre></div><!----> <p>提供计划不会推断并行大小:请显式设置 <code>tp_size</code> 和 <code>ep_size</code>。</p> <p>当 <code>ep_size=tp_size</code> 时,默认的 <code>"auto"</code> 实现在推理时执行 <code>"all_reduce"</code>,并在训练时分发 token。若要在训练中也进行掩码和 all-reduce,请设置 <code>ep_implementation="all_reduce"</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 "><!---->distributed_config = DistributedConfig(tp_size=<span class="hljs-number">4</span>, ep_size=<span class="hljs-number">4</span>, ep_implementation=<span class="hljs-string">&quot;all_reduce&quot;</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="token-dispatch" 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="#token-dispatch"><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><!---->Token 分发<!----></span></h2><!--]--><!----> <p>Token dispatch 是 <code>"all_to_all"</code> 的实现方式:每个 rank 仅训练其负责的批次部分。在每一层 MoE 中,一个 rank 会对其 token 进行路由,通过 all-to-all 将每个(token, expert)对发送到拥有该 expert 的 rank,在本地运行其接收到的 expert,再通过第二次 all-to-all 取回结果,并使用路由权重进行组合。只有被路由的激活值和 expert 输出会传输,且没有任何 rank 会计算其不拥有的 token 的 expert。</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> AutoModelForCausalLM
<span class="hljs-keyword">from</span> transformers.distributed <span class="hljs-keyword">import</span> DistributedConfig
distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">1</span>,
fsdp_size=<span class="hljs-number">8</span>,
ep_size=<span class="hljs-number">4</span>,
)
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen3-30B-A3B&quot;</span>, distributed_config=distributed_config)<!----></pre></div><!----> <p>当 <code>tp_size=1</code> 时,<code>ep_size</code> 必须能整除 <code>fsdp_size</code> 和专家数量,且注意力机制不受 <code>num_key_value_heads</code> 的限制。对于模型的其余部分:</p> <ul><li>专家之外的参数通过 <a href="./fsdp">FSDP2</a> 在 <code>fsdp</code> 维度上进行分片,从而减少其梯度。</li> <li>专家参数在 <code>ep</code> 上进行分片,并且当 <code>efsdp_size = fsdp_size * tp_size // ep_size</code> 大于 1 时,还会额外在 <code>efsdp</code> 上进行 FSDP 分片。它们始终被 FSDP 包装,因此 <code>fsdp_mixed_precision</code> 和 <code>fsdp_cpu_offload</code> 同样适用于它们,并且 <a href="/docs/transformers/main/zh/main_classes/model#transformers.PreTrainedModel.save_pretrained">save_pretrained()</a> 会像处理其他参数一样将它们聚合。</li> <li>一个专家并行组包含 <code>ep_size / tp_size</code> 个批次,因此某个专家的梯度是这些批次上的求和。<code>efsdp</code> 的归约操作除以的是 <code>fsdp_size</code> 而非其组大小,这与 FSDP2 对稠密模块所采用的每批次平均值相同。</li> <li>每个本地专家在每一层中还会处理一行零填充。即使某个 rank 的专家未接收到任何 token,它仍会参与反向 all-to-all 通信以及专家梯度归约。</li></ul> <!--[2--><h3 class="relative group"><a id="how-the-sizes-combine" 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="#how-the-sizes-combine"><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></h3><!--]--><!----> <ul><li><code>tp_size * fsdp_size</code> 是进程数。<code>ep_size</code> 不会增加进程数:它仅针对专家权重重新组合相同的 rank。</li> <li><code>ep_size</code> cuts the expert list into <code>ep_size</code> blocks. Each rank computes <code>num_experts / ep_size</code> experts, and <code>ep_size</code> consecutive ranks hold one complete set. That set of ranks is the group the all-to-all runs in.</li> <li><code>efsdp_size = fsdp_size * tp_size / ep_size</code> 表示专家集的完整副本数量。位于不同副本中相同位置的 rank 会像 FSDP 对稠密模块所做的那样,对这些专家进行分片以节省内存,并对它们的梯度求平均。</li> <li>某个 rank 持有的批次仅取决于 <code>fsdp</code>。连续的 rank 组成一个 TP 组并获取相同的批次;<code>fsdp_size</code> 个组则获取不同的批次。</li></ul> <p>八个进程,<code>tp_size=2, fsdp_size=4</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-text "><!---->rank 0 1 2 3 4 5 6 7
batch [====B0====] [====B1====] [====B2====] [====B3====] one batch per TP pair
tp 0 1 0 1 0 1 0 1
ep_size=2 E0-3 E4-7 E0-3 E4-7 E0-3 E4-7 E0-3 E4-7 group = a TP pair, efsdp_size=4
ep_size=4 E0E1 E2E3 E4E5 E6E7 E0E1 E2E3 E4E5 E6E7 group = two pairs, efsdp_size=2
ep_size=8 E0 E1 E2 E3 E4 E5 E6 E7 group = all ranks, efsdp_size=1<!----></pre></div><!----> <p>从图中可以得出两个数字:</p> <ul><li>在 EP 组内,每个 token 存在 <code>tp_size</code> 份,持有其批次的每一对 rank 各持有一份。这与 <code>ep_size</code> 无关。</li> <li>EP 组持有 <code>ep_size / tp_size</code> 个不同的批次。这是单个专家的梯度所累加的批次数量。</li></ul> <!--[2--><h3 class="relative group"><a id="with-tensor-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="#with-tensor-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></h3><!--]--><!----> <p>将 <code>tp_size > 1</code> 设置为大于 1,以使用 TP 计划对稠密模块进行分片,同时让专家模块使用 dispatch。在八个进程上:</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 "><!---->distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">2</span>,
fsdp_size=<span class="hljs-number">4</span>,
ep_size=<span class="hljs-number">4</span>,
)<!----></pre></div><!----> <p>由于张量并行在 TP rank 对内部复制了激活值,因此每对 TP rank 接收相同的 batch。如果两个 rank 都分发了它们所有的 token,那么拥有某个专家的 rank 将会收到每个 token 两次,对其进行两次计算,并且其权重梯度会翻倍。专家完整地存在于单个 rank 上,因此无法通过权重来拆分重复部分;而且拥有者通常是另一个 rank,因此也无法像 masking 那样通过所有权来解决。因此,该对 rank 会拆分行:每个 TP rank 分发互不重叠的 <code>1 / tp_size</code> 的 token,结果返回给发送它们的 rank,然后对零填充的半部分在该对 rank 之间执行 all-reduce,以恢复下一层所期望的复制输出。拆分依据是 <code>tp_size</code>,而不是 <code>ep_size</code>,因为只有持有 batch 的 rank 才能发送它。专家组跨越四个 rank,每个专家在 <code>efsdp_size = 2</code> 个 rank 之间进行 FSDP 分片,而主干的 FSDP 组跨越四个 rank。模型常规的 TP 约束(例如注意力头数的可整除性)仍然适用于稠密模块。Token 切片可能不均匀或为空,包括在单 token 解码期间。</p> <p>Token dispatch 目前尚不能与流水线并行结合使用;请使用 <code>pp_size=1</code>(尚未测试)
这些配置均使用八块 GPU:</p> <table><thead><tr><th align="left">配置</th><th align="left">结果</th></tr></thead><tbody><tr><td align="left"><code>DistributedConfig(tp_size=4, fsdp_size=2, ep_size=4)</code></td><td align="left">每个由四个进程组成的 TP 组共享一个批次。训练时,令牌被分发,每个 rank 处理批次的四分之一;推理时使用 <code>"all_reduce"</code>;设置 <code>ep_implementation="all_reduce"</code> 后,整个流程都会使用它。</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=1, fsdp_size=8, ep_size=4)</code></td><td align="left">在每个 rank 上使用独立的 batch 进行分发,不进行切片,并将专家模型通过 FSDP 在 rank 对之间进行分片。</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=2, fsdp_size=4, ep_size=4)</code></td><td align="left">按每个批次一个 TP 对进行分发,每个对将其批次一分为二;每个专家组包含两个批次。</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=8, ep_size=8)</code></td><td align="left">每个 rank 共享一个 batch。训练时分发 token,每个 rank 处理 batch 的八分之一;推理时使用 <code>"all_reduce"</code>;设置 <code>ep_implementation="all_reduce"</code> 后,整个流程都会使用它。</td></tr></tbody></table> <blockquote class="warning"><p><a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer">Trainer</a> 目前尚未考虑 token dispatch:batch 和 token 计数假设采用 all-reduce 布局,即 TP 组内的各个 rank 共享一个 batch,并且存在 <code>fsdp_size</code> 个数据并行分片。Trainer 对 dispatch 的支持将在后续版本中提供。</p></blockquote> <!--[1--><h2 class="relative group"><a id="combining-with-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="#combining-with-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></h2><!--]--><!----> <p>张量并行和专家并行分别在各自的网格 <code>tp</code> 和 <code>ep</code> 上对权重进行分片,但优化器状态以及没有规则的模块仍在组内的每个 rank 上保持复制,这限制了可训练模型的规模。在另一个网格维度上通过 <code>fsdp_size</code> 添加 <a href="./fsdp">FSDP2</a>。使用掩码和 all-reduce 时,保持 <code>ep_size=tp_size</code> 并传入 <code>ep_implementation="all_reduce"</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> AutoModelForCausalLM
<span class="hljs-keyword">from</span> transformers.distributed <span class="hljs-keyword">import</span> DistributedConfig
distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">4</span>,
ep_size=<span class="hljs-number">4</span>, <span class="hljs-comment"># expert parallel size, must match tp_size with masking and all-reduce</span>
fsdp_size=<span class="hljs-number">2</span>, <span class="hljs-comment"># data parallel shards</span>
ep_implementation=<span class="hljs-string">&quot;all_reduce&quot;</span>, <span class="hljs-comment"># &quot;auto&quot; would dispatch tokens in training, which the Trainer does not support yet</span>
)
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen3-30B-A3B&quot;</span>, distributed_config=distributed_config)<!----></pre></div><!----> <p>按常规方式加载模型,然后使用 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer">Trainer</a> 进行训练。它会计算两个 mesh 上的梯度范数,并为每个 mesh 分配独立的优化器参数组。<a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer.save_model">save_model()</a> 会将分片权重聚合为一个常规检查点。这需要 <code>accelerate>=1.12</code>,以便 <code>Trainer</code> 能够将 <code>tp_size</code> 和 <code>fsdp_size</code> 镜像到 <code>~Accelerate.ParallelismConfig</code> 中。</p> <p>下表对比了在 8xH100 GPU 上仅使用 EP 的训练与 2D EP+FSDP2 的训练。工作负载为在 bf16 精度、序列长度 2048 下对 Qwen3-30B-A3B 进行全量微调。更多的 FSDP 分片会降低峰值内存,但由于 FSDP2 会在 <code>fsdp</code> 中对专家进行 all-gather 和 reduce-scatter,tokens/s 会略有下降。</p> <table><thead><tr><th>配置</th><th>tokens/s/GPU</th><th>峰值内存/GPU</th></tr></thead><tbody><tr><td><code>tp_size=8</code></td><td>3485</td><td>38.6 GB</td></tr><tr><td><code>tp_size=4, fsdp_size=2</code></td><td>2900</td><td>34.2 GB</td></tr><tr><td><code>tp_size=2, fsdp_size=4</code></td><td>2830</td><td>32.3 GB</td></tr></tbody></table> <blockquote class="warning"><p>对于在加载时进行分片的模型,尚不支持从检查点恢复,因此 <a href="/docs/transformers/main/zh/main_classes/trainer#transformers.Trainer">Trainer</a> 仅接受 <code>save_only_model=True</code> 或 <code>save_strategy="no"</code>。</p></blockquote> <!--[1--><h2 class="relative group"><a id="transformers.DistributedConfig" 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="#transformers.DistributedConfig"><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><!---->API 参考<!----></span></h2><!--]--><!----> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><div><span class="group flex space-x-1.5 items-center text-gray-800 bg-gradient-to-r rounded-tr-lg -mt-4 -ml-4 pt-3 px-2.5" id="transformers.DistributedConfig"><!----><h3 class="!m-0"><span class="flex-1 break-all md:text-lg bg-gradient-to-r px-2.5 py-1.5 rounded-xl from-indigo-50/70 to-white dark:from-gray-900 dark:to-gray-950 dark:text-indigo-300 text-indigo-700"><svg class="mr-1.5 text-indigo-500 inline-block -mt-0.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width=".8em" height=".8em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg><span class="font-light">类</span> <span class="font-medium">transformers.</span><span class="font-semibold">DistributedConfig</span></span></h3><!----> <a id="transformers.DistributedConfig" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.DistributedConfig"><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><!----></a> <!--[0--><a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/main/src/transformers/distributed/configuration_utils.py#L26" target="_blank"><span>&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline">source</span> <span>></span></a><!--]--></span> <!--[0--><p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span>(</span> <!--[--><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">tp_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">tp_plan<span class="opacity-60">: typing.Union[dict[str, str], typing.Literal['auto'], NoneType] = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">enable_sequence_parallel<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">enable_expert_parallel<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">dp_replicate_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_plan<span class="opacity-60">: dict[str, str] | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_cpu_offload<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_mixed_precision<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">pp_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">ep_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">ep_plan<span class="opacity-60">: dict[str, str] | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">ep_implementation<span class="opacity-60">: str = 'auto'</span></span></span><!--]--> <span>)</span> <!--[-1--><!--]--></p><!--]--> <div class="!mb-10 relative docstring-details "><!--[-1--><!--]--> <!--[0--><p class="flex items-center font-semibold !mt-2 !mb-2 text-gray-800">参数 <span class="flex-auto border-t-2 border-gray-100 dark:border-gray-700 ml-3"></span></p> <ul class="px-2"><!--[--><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.tp_size" class="header-link block pr-0.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="#transformers.DistributedConfig.tp_size"><span><svg class="text-smd" 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><!----><strong>tp_size</strong> (<code>int</code>, <em>可选</em>) —
用于张量并行的设备数量。如果 <code>None</code> 且设置了 <code>tp_plan</code>,则默认为
<code>WORLD_SIZE // (other_parallel_size)</code>。如果 <code>None</code> 且未设置 <code>tp_plan</code>,则默认为 1。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.tp_plan" class="header-link block pr-0.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="#transformers.DistributedConfig.tp_plan"><span><svg class="text-smd" 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><!----><strong>tp_plan</strong> (<code>dict[str, str]</code> 或 <code>&quot;auto&quot;</code>,<em>可选</em>) —
张量并行分片计划。当 <code>tp_size</code> 已设置时,传入 <code>&quot;auto&quot;</code> 或保持为 <code>None</code>,即可使用模型预定义的 <code>base_model_tp_plan</code>。传入字典可覆盖该计划中的个别规则;未指定的规则将保留。空字典表示不应用任何张量并行规则。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.enable_sequence_parallel" class="header-link block pr-0.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="#transformers.DistributedConfig.enable_sequence_parallel"><span><svg class="text-smd" 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><!----><strong>enable_sequence_parallel</strong> (<code>bool</code>,<em>可选</em>,默认为 <code>False</code>) —
保留用于序列并行。尚未接入。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.enable_expert_parallel" class="header-link block pr-0.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="#transformers.DistributedConfig.enable_expert_parallel"><span><svg class="text-smd" 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><!----><strong>enable_expert_parallel</strong> (<code>bool</code>,<em>可选</em>,默认为 <code>False</code>) —
已弃用,将在 v5.21 中移除。在未指定 <code>ep_size</code> 的情况下,其效果等同于 <code>ep_size=tp_size, tp_plan={}</code>:仅在 <code>tp_size</code> 个 rank 上应用专家并行方案。显式指定的 <code>ep_size</code> 具有更高优先级。此标志不会更改 <code>tp_size</code> 或 <code>fsdp_size</code>。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_size" class="header-link block pr-0.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="#transformers.DistributedConfig.fsdp_size"><span><svg class="text-smd" 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><!----><strong>fsdp_size</strong> (<code>int</code>, <em>可选</em>) —
用于 FSDP(数据并行)的设备数量。如果 <code>None</code> 且设置了 <code>tp_size</code>,则默认为 1。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.dp_replicate_size" class="header-link block pr-0.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="#transformers.DistributedConfig.dp_replicate_size"><span><svg class="text-smd" 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><!----><strong>dp_replicate_size</strong> (<code>int</code>, <em>可选</em>) —
FSDP 副本的数量(HSDP)。参数在 <code>fsdp_size</code> 个设备上进行分片,并复制 <code>dp_replicate_size</code> 份:梯度先在副本内部进行 reduce-scatter,然后在副本之间进行 all-reduce。默认值为 1。我们建议将其用于多节点训练:普通的 FSDP 会在每一步中跨所有设备对参数进行 all-gather,并对梯度进行 reduce-scatter,因此这些流量会经过较慢的节点间链路。将 <code>dp_replicate_size</code> 设置为节点数,并将 <code>fsdp_size * tp_size</code> 设置为每个节点的设备数:这样分片流量就会保留在快速的节点内链路上,只有梯度 all-reduce 会跨节点传输。其代价是每个设备的内存占用会增加,因为参数仅在节点内进行分片。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_plan" class="header-link block pr-0.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="#transformers.DistributedConfig.fsdp_plan"><span><svg class="text-smd" 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><!----><strong>fsdp_plan</strong> (<code>dict[str, str]</code>, <em>可选</em>) —
FSDP2 分片计划。保持为 <code>None</code> 以使用模型预定义的 <code>base_model_fsdp_plan</code> 和类级别的 <code>_fsdp_plan</code>。传入字典以覆盖个别规则;未指定的规则将保留。仅在 <code>dp_replicate_size * fsdp_size &gt; 1</code> 时应用。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_cpu_offload" class="header-link block pr-0.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="#transformers.DistributedConfig.fsdp_cpu_offload"><span><svg class="text-smd" 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><!----><strong>fsdp_cpu_offload</strong> (<code>bool</code>,<em>可选</em>,默认为 <code>False</code>) —
是否启用 FSDP2 的 CPU 卸载。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_mixed_precision" class="header-link block pr-0.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="#transformers.DistributedConfig.fsdp_mixed_precision"><span><svg class="text-smd" 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><!----><strong>fsdp_mixed_precision</strong> (<code>bool</code>,<em>可选</em>,默认为 <code>False</code>) —
是否为 FSDP2 启用混合精度。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.pp_size" class="header-link block pr-0.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="#transformers.DistributedConfig.pp_size"><span><svg class="text-smd" 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><!----><strong>pp_size</strong> (<code>int</code>, <em>可选</em>) —
用于流水线并行的设备数量。如果 <code>None</code> 且设置了其他并行模式,则默认为 1。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.ep_size" class="header-link block pr-0.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="#transformers.DistributedConfig.ep_size"><span><svg class="text-smd" 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><!----><strong>ep_size</strong> (<code>int</code>, <em>可选</em>) —
拥有不同专家分片的设备数量。默认值为 1。需显式设置以启用 EP。必须是 <code>tp_size</code> 的倍数,且能整除 <code>fsdp_size * tp_size</code>。当 <code>ep_size &gt; tp_size</code> 时,需要一个 <code>&quot;ep_dispatch_experts&quot;</code> 规则,并指定一个用于交换 token 的 <code>ep_implementation</code>,例如 <code>&quot;all_to_all&quot;</code>。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.ep_plan" class="header-link block pr-0.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="#transformers.DistributedConfig.ep_plan"><span><svg class="text-smd" 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><!----><strong>ep_plan</strong> (<code>dict[str, str]</code>, <em>可选</em>) —
专家并行分片计划。保持为 <code>None</code> 以使用模型预定义的 <code>base_model_ep_plan</code>。传入一个
字典以覆盖该计划中的个别规则;未指定的规则将保留。仅在
<code>ep_size &gt; 1</code> 时应用,且对于相同的模块,其规则优先于 <code>tp_plan</code> 规则。
<code>&quot;ep_dispatch_experts&quot;</code> 规则允许 <code>ep_implementation</code> 决定令牌如何到达专家。<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.ep_implementation" class="header-link block pr-0.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="#transformers.DistributedConfig.ep_implementation"><span><svg class="text-smd" 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><!----><strong>ep_implementation</strong> (<code>str</code>, <em>可选</em>, 默认为 <code>&quot;auto&quot;</code>) —
描述 <code>&quot;ep_dispatch_experts&quot;</code> 规则如何将 token 分发给专家。它指定了在 <code>EPImplementation</code>
中注册的:"all_to_all" 通过 all-to-all 集合通信交换路由后的 token。"all_reduce" 在每个 rank 上对 EP 组共享的 batch 运行专家,并对输出求和。它要求 ep_size == tp_size。"deepgemm_megamoe" 将该 batch 交给 DeepGEMM Mega MoE,由其内核自行交换 token。加载时设置 experts_implementation="deepgemm_megamoe" 即可选择它。"auto" 在推理时,若 EP 组共享同一个 batch 则运行 "all_reduce",否则运行 "all_to_all"。 <code>EPImplementationInterface</code>:
<ul>
<li><code>&quot;all_to_all&quot;</code> 通过 all-to-all 集合通信交换路由后的 token。</li>
<li><code>&quot;all_reduce&quot;</code> 在每个 EP 组共享的批次上运行每个 rank 的专家,并对输出求和。它要求 <code>ep_size == tp_size</code>。</li>
<li><code>&quot;deepgemm_megamoe&quot;</code> 将该批次交给 DeepGEMM Mega MoE,其内核自行交换 token。
使用 <code>experts_implementation=&quot;deepgemm_megamoe&quot;</code> 加载时会选择它。</li>
<li><code>&quot;auto&quot;</code> 在推理时,如果 EP 组共享同一个批次,则执行 <code>&quot;all_reduce&quot;</code>,否则执行 <code>&quot;all_to_all&quot;</code>。</li>
</ul><!----></span></span></li><!--]--></ul><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--></div></div><!----> <p>用于原生分布式推理和训练的配置,支持张量、流水线或 FSDP2 并行。</p></div> <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/expert_parallelism.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_p1l6wl = {
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.DS27pc2p.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 5],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
77.9 kB
·
Xet hash:
79aa3eaedd6ef6b7e5482656e1c881a8c9c6f07c2d371b457d19bdca51971c17

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