Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Experts 后端","local":"experts-backends","sections":[{"title":"解码阶段切换","local":"decode-stage-switching","sections":[],"depth":2},{"title":"设置 experts 后端","local":"set-an-experts-backend","sections":[],"depth":2},{"title":"特定骨干的专家后端","local":"backbone-specific-experts-backend","sections":[],"depth":2},{"title":"DeepGEMM","local":"deepgemm","sections":[{"title":"FP8 和 FP4 量化专家","local":"fp8-and-fp4-quantized-experts","sections":[],"depth":3},{"title":"Blackwell 上的融合 Mega MoE","local":"fused-mega-moe-on-blackwell","sections":[],"depth":3}],"depth":2},{"title":"SonicMoE","local":"sonicmoe","sections":[],"depth":2},{"title":"torch.compile","local":"torchcompile","sections":[],"depth":2},{"title":"基准测试","local":"benchmarks","sections":[],"depth":2}],"depth":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/51.lsRCauwt.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload"> | |
| <!--1navn4e--><meta name="hf:doc:metadata" content="{"title":"Experts backends","local":"experts-backends","sections":[{"title":"Decode-stage switching","local":"decode-stage-switching","sections":[],"depth":2},{"title":"Set an experts backend","local":"set-an-experts-backend","sections":[],"depth":2},{"title":"Backbone-specific experts backend","local":"backbone-specific-experts-backend","sections":[],"depth":2},{"title":"DeepGEMM","local":"deepgemm","sections":[{"title":"FP8 and FP4 quantized experts","local":"fp8-and-fp4-quantized-experts","sections":[],"depth":3},{"title":"Fused Mega MoE on Blackwell","local":"fused-mega-moe-on-blackwell","sections":[],"depth":3}],"depth":2},{"title":"SonicMoE","local":"sonicmoe","sections":[],"depth":2},{"title":"torch.compile","local":"torchcompile","sections":[],"depth":2},{"title":"Benchmarks","local":"benchmarks","sections":[],"depth":2}],"depth":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="experts-backends" 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="#experts-backends"><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><!---->Experts 后端<!----></span></h1><!--]--><!----> <p>所有 Mixture-of-Experts (MoE) 实现都执行相同的高层计算。对于每个 token,路由器会选择 <em>k</em> 个专家。随后,token 的隐藏状态会通过所选专家的参数进行投影,并根据路由权重进行聚合。不同专家后端之间的区别在于这些专家矩阵乘法是<em>如何</em>执行的。</p> <p><code>ExpertsInterface</code> 提供了经过优化的专家后端。它将专家的实现与模型代码解耦,从而简化了对不同函数的实验。通过同一接口添加新的后端。</p> <table><thead><tr><th>experts 后端</th><th>描述</th><th>GPU</th><th>CPU</th></tr></thead><tbody><tr><td><code>"eager"</code></td><td>参考实现,遍历选定的专家,并对它们的 token 应用投影。</td><td>无需编译即可实现合理的基线性能。</td><td>比 <code>grouped_mm</code> 慢,但比 <code>batched_mm</code> 快。</td></tr><tr><td><code>"batched_mm"</code></td><td>为每个 token 复制所选的专家参数,并使用 <a href="https://docs.pytorch.org/docs/stable/generated/torch.bmm.html" rel="nofollow">torch.bmm</a> 在单个批量 GEMM 中投影所有 token。</td><td>对于小型输入,尤其是启用编译时,速度最快。由于参数复制,会占用更多内存。</td><td>不推荐(比其他后端显著更慢)。</td></tr><tr><td><code>"grouped_mm"</code></td><td>按选定的专家对 token 进行排序,并使用 <a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.grouped_mm.html" rel="nofollow">torch.nn.functional.grouped_mm</a> 通过单次分组 GEMM 投影所有 token(需要 PyTorch 2.9+)。</td><td>适用于较大的输入,并且由于避免了重复专家参数,因此内存效率更高。配合编译使用时速度很快。</td><td>适用于所有输入尺寸的最高效后端。</td></tr><tr><td><code>"deepgemm"</code></td><td>按所选专家对 token 进行排序,并使用来自 <a href="https://huggingface.co/kernels-community/deep-gemm" rel="nofollow">kernels-community/deep-gemm</a> 的 <a href="https://github.com/deepseek-ai/DeepGEMM" rel="nofollow">DeepGEMM</a> 内核,通过单次 TMA 对齐的分组 GEMM 投影所有 token。</td><td>DeepSeek 模型在 Hopper (SM90+) 和 Blackwell (SM100+) 上的原生后端;支持 <code>bfloat16</code> 以及 FP8/FP4 量化专家。</td><td>不支持(仅限 CUDA)。</td></tr><tr><td><code>"deepgemm_megamoe"</code></td><td>将专家并行分发、门控 MLP(上投影、SwiGLU、下投影)以及 EP 合并融合为单个 DeepGEMM Mega MoE 内核,使 NVLink 传输与张量核心计算相互重叠。</td><td>仅适用于 Blackwell(SM100+),用于以专家并行方式运行的 FP4 量化专家。</td><td>不支持(仅限 CUDA)。</td></tr><tr><td><code>"sonicmoe"</code></td><td>将路由的 <code>bfloat16</code> MoE 前向传播(路由器分发、门控上投影、激活、下投影)融合为来自 <a href="https://huggingface.co/kernels-community/sonic-moe" rel="nofollow">kernels-community/sonic-moe</a> 的 CuteDSL 分组 GEMM 内核(源自 <a href="https://github.com/Dao-AILab/quack" rel="nofollow">quack</a> 库)。</td><td>在 Hopper(SM90+)上,针对采用门控激活函数(SwiGLU/GeGLU/ReGLU)的 <code>bfloat16</code> 专家,实现了最先进的吞吐量,尤其适用于训练场景。</td><td>不支持(仅限 CUDA)。</td></tr></tbody></table> <p><code>"batched_mm"</code> 和 <code>"grouped_mm"</code> 后端同样通过 Triton finegrained-fp8 内核运行 FP8 和 FP4(<code>int8</code> 打包)量化专家,读取 <code>float32</code> 或 UE8M0 缩放因子。当 <code>"deepgemm"</code> 后端不可用时,它们作为量化检查点的回退方案。</p> <!--[1--><h2 class="relative group"><a id="decode-stage-switching" 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="#decode-stage-switching"><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>在 GPU 上,使用 <code>experts_implementation="grouped_mm"</code> 加载的模型会在生成过程的解码阶段自动切换到 <code>"batched_mm"</code>,这在处理较少 token 时速度显著提升。生成结束后,系统会恢复为原始后端。在 CPU 上,<code>grouped_mm</code> 在整个生成过程中保持激活状态,因为它在任何输入规模下都更高效。</p> <p>该开关会作用于顶层模型以及任意子配置主干中的 MoE 层,例如视觉-语言模型的 <code>text_config</code>。只有 <code>grouped_mm</code> 条目会切换为 <code>batched_mm</code>。运行其他后端的专家将保持原样。</p> <!--[1--><h2 class="relative group"><a id="set-an-experts-backend" 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="#set-an-experts-backend"><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><!---->设置 experts 后端<!----></span></h2><!--]--><!----> <p>使用 <a href="/docs/transformers/main/zh/main_classes/model#transformers.PreTrainedModel.from_pretrained">from_pretrained()</a> 中的 <code>experts_implementation</code> 参数,可以实例化一个使用特定 experts 后端的模型。</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 | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen1.5-MoE-A2.7B"</span>, | |
| dtype=<span class="hljs-string">"bfloat16"</span>, | |
| experts_implementation=<span class="hljs-string">"batched_mm"</span>, | |
| )<!----></pre></div><!----> <p>使用 <a href="/docs/transformers/main/zh/main_classes/model#transformers.PreTrainedModel.set_experts_implementation">set_experts_implementation()</a> 可在运行时切换专家后端,而无需重新加载模型。</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 "><!---->model.set_experts_implementation(<span class="hljs-string">"eager"</span>)<!----></pre></div><!----> <p>使用 <a href="/docs/transformers/main/zh/main_classes/model#transformers.PreTrainedModel.get_experts_implementation">get_experts_implementation()</a> 读取当前正在运行的后端。它返回一个 <code>dict</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 "><!---->model.get_experts_implementation() | |
| <span class="hljs-comment"># {"": "grouped_mm", "text_config": "grouped_mm", "vision_config": "eager"}</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="backbone-specific-experts-backend" 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="#backbone-specific-experts-backend"><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>多模态模型可以拥有多个子配置(例如,不同的骨干网络)。在加载时,通过向 <code>experts_implementation</code> 传入一个 <code>dict</code>,可以为每个子配置设置不同的专家后端。</p> <p>映射中的键必须与子配置名称匹配。</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> AutoModelForImageTextToText | |
| experts_implementation_per_backbone = { | |
| <span class="hljs-string">"text_config"</span>: <span class="hljs-string">"grouped_mm"</span>, | |
| <span class="hljs-string">"vision_config"</span>: <span class="hljs-string">"eager"</span>, | |
| } | |
| model = AutoModelForImageTextToText.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen3-VL-Moe"</span>, | |
| experts_implementation=experts_implementation_per_backbone, | |
| )<!----></pre></div><!----> <p>使用空键全局设置 experts 后端。</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 "><!---->model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen1.5-MoE-A2.7B"</span>, | |
| experts_implementation={<span class="hljs-string">""</span>: <span class="hljs-string">"batched_mm"</span>}, | |
| )<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="deepgemm" 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="#deepgemm"><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><!---->DeepGEMM<!----></span></h2><!--]--><!----> <p><code>"deepgemm"</code> 后端通过 <a href="https://huggingface.co/kernels-community/deep-gemm" rel="nofollow">kernels-community/deep-gemm</a> 分发的 <a href="https://github.com/deepseek-ai/DeepGEMM" rel="nofollow">DeepGEMM</a> 内核来路由专家矩阵乘法。它支持未量化的 <code>bfloat16</code> 专家,以及通过 <a href="./quantization/finegrained_fp8">Fine-grained FP8</a> 加载的 FP8/FP4 量化专家。</p> <p><code>"deepgemm"</code> 后端需要:</p> <ul><li>计算能力 ≥ 9.0 的 CUDA GPU(Hopper 或更新架构)。</li> <li>完整的 CUDA 工具包,包含用于内核 JIT 编译的 <code>nvcc</code>,在 Hopper 上版本需为 12.3 或更高,在 Blackwell 上需为 12.9 或更高。仅包含运行时的安装没有 <code>nvcc</code>,会被拒绝。Transformers 通过 <code>CUDA_HOME</code>、<code>CUDA_PATH</code>、<code>PATH</code> 上的 <code>nvcc</code>,然后 <code>/usr/local/cuda</code> 来定位工具包。</li> <li><a href="https://github.com/huggingface/kernels" rel="nofollow">kernels</a> 包。</li></ul> <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 | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"deepseek-ai/DeepSeek-V3"</span>, | |
| dtype=<span class="hljs-string">"bfloat16"</span>, | |
| experts_implementation=<span class="hljs-string">"deepgemm"</span>, | |
| )<!----></pre></div><!----> <p>内核会在第一次前向传播时延迟加载。</p> <!--[2--><h3 class="relative group"><a id="fp8-and-fp4-quantized-experts" 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="#fp8-and-fp4-quantized-experts"><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><!---->FP8 和 FP4 量化专家<!----></span></h3><!--]--><!----> <p>DeepSeek 风格的检查点通常已预先量化,并自带量化配置,因此无需传入 <a href="/docs/transformers/main/zh/main_classes/quantization#transformers.FineGrainedFP8Config">FineGrainedFP8Config</a>。<code>"deepgemm"</code> 后端会自动选择 FP8(或在 Blackwell 上选择 FP4)分组 GEMM 内核。DeepGEMM 要求使用动态逐行激活缩放(<code>activation_scheme="dynamic"</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 | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"deepseek-ai/DeepSeek-V3"</span>, | |
| experts_implementation=<span class="hljs-string">"deepgemm"</span>, | |
| )<!----></pre></div><!----> <p>对于 FP4 打包的专家权重(DeepSeek V4 风格),GPU 必须是 SM100+(Blackwell)。检查点配置通常设置为 <code>expert_dtype="fp4"</code> 和 <code>scale_fmt="ue8m0"</code>。</p> <blockquote class="note"><p>在 Blackwell(SM100+)上,<code>"deepgemm"</code> 和 <code>"deepgemm_megamoe"</code> 专家内核要求使用 2 的幂次 UE8M0 专家缩放因子。使用普通 <code>float32</code> 缩放因子(<code>scale_fmt="float"</code>)量化的检查点,在首次前向传播时会抛出 <code>ValueError</code>,而不是静默地损坏输出。请加载使用 <code>scale_fmt="ue8m0"</code> 量化的检查点,或改用 <code>grouped_mm</code> 或 <code>batched_mm</code>,它们可以直接使用 <code>float32</code> 块缩放因子。Hopper(SM90+)在 DeepGEMM 路径上直接使用 <code>float32</code> 缩放因子,无需转换。</p></blockquote> <p>为预量化检查点传入 <a href="/docs/transformers/main/zh/main_classes/quantization#transformers.FineGrainedFP8Config">FineGrainedFP8Config</a> 的主要目的是将其反量化回 <code>bfloat16</code>,在这种情况下,专家模块将以 <code>bfloat16</code> 运行,而不是走 FP8/FP4 DeepGEMM 路径。</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, FineGrainedFP8Config | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"deepseek-ai/DeepSeek-V3"</span>, | |
| quantization_config=FineGrainedFP8Config(dequantize=<span class="hljs-literal">True</span>), | |
| experts_implementation=<span class="hljs-string">"deepgemm"</span>, | |
| )<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="fused-mega-moe-on-blackwell" 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="#fused-mega-moe-on-blackwell"><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><!---->Blackwell 上的融合 Mega MoE<!----></span></h3><!--]--><!----> <p>在 Blackwell(SM100+)上,设置 <code>experts_implementation="deepgemm_megamoe"</code> 以运行一个融合内核,该内核将专家并行分发、上投影、SwiGLU、下投影以及 EP 合并组合在一起,并将 NVLink 传输与张量核心计算重叠执行。</p> <p>此后端需要:</p> <ul><li>配备 CUDA 工具包(<code>nvcc</code>)12.9 或更高版本的 Blackwell GPU(计算能力 ≥ 10.0)。</li> <li>FP4 打包的专家权重,搭配 UE8M0 权重缩放因子(预量化检查点通常在其配置中声明 <code>expert_dtype="fp4"</code> 和 <code>scale_fmt="ue8m0"</code>)。</li> <li><code>torch.distributed</code> 进程组,用于专家并行组。当 <code>ep_size > 1</code> 时,专家并行封装会自动提供该进程组。</li></ul> <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">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, DistributedConfig | |
| distributed_config = DistributedConfig( | |
| tp_size=<span class="hljs-built_in">int</span>(os.environ[<span class="hljs-string">"WORLD_SIZE"</span>]), | |
| ep_size=<span class="hljs-built_in">int</span>(os.environ[<span class="hljs-string">"WORLD_SIZE"</span>]), | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"deepseek-ai/DeepSeek-V4"</span>, | |
| experts_implementation=<span class="hljs-string">"deepgemm_megamoe"</span>, | |
| distributed_config=distributed_config, | |
| )<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="sonicmoe" 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="#sonicmoe"><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><!---->SonicMoE<!----></span></h2><!--]--><!----> <p><code>"sonicmoe"</code> 后端将路由式 MoE 的前向传播(dispatch、gated up projection、activation、down projection)融合为一组高度优化的 CuteDSL grouped-GEMM 内核,该内核基于 <a href="https://github.com/Dao-AILab/quack" rel="nofollow">quack</a> 库构建,并由 <a href="https://huggingface.co/kernels-community/sonic-moe" rel="nofollow">kernels-community/sonic-moe</a> 分发。</p> <p><code>"sonicmoe"</code> 后端需要:</p> <ul><li>计算能力 ≥ 9.0 的 CUDA GPU(Hopper 或更新架构)。</li> <li><a href="https://github.com/huggingface/kernels" rel="nofollow">kernels</a> 包和 <code>nvidia-cutlass-dsl</code> 包。</li> <li>采用门控激活(<code>silu</code>、<code>gelu</code> 或 <code>relu</code>,分别映射为 SwiGLU/GeGLU/ReGLU)的专家。</li></ul> <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 | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen1.5-MoE-A2.7B"</span>, | |
| dtype=<span class="hljs-string">"bfloat16"</span>, | |
| experts_implementation=<span class="hljs-string">"sonicmoe"</span>, | |
| )<!----></pre></div><!----> <p>如果未满足要求,forward 会抛出 <code>ImportError</code>,此时应选择一个不同的 <code>experts_implementation</code>。</p> <!--[1--><h2 class="relative group"><a id="torchcompile" 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="#torchcompile"><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><!---->torch.compile<!----></span></h2><!--]--><!----> <p><code>"eager"</code>、<code>"batched_mm"</code> 和 <code>"grouped_mm"</code> 后端在某种程度上与 <code>torch.compile</code> 兼容。下表总结了它们的兼容性。<code>"deepgemm"</code>、<code>"deepgemm_megamoe"</code> 和 <code>"sonicmoe"</code> 后端通过外部 CUDA 内核进行路由,因此不在本表的覆盖范围内。</p> <table><thead><tr><th>实现</th><th>编译模式</th><th>数据类型</th><th><code>fullgraph=True</code></th></tr></thead><tbody><tr><td><code>grouped_mm</code></td><td><code>None</code>, <code>max-autotune-no-cudagraphs</code></td><td><code>bfloat16</code></td><td>是的</td></tr><tr><td><code>grouped_mm</code>(回退)</td><td><code>None</code>, <code>max-autotune-no-cudagraphs</code></td><td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td><td>是的</td></tr><tr><td><code>batched_mm</code></td><td>全部</td><td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td><td>是的</td></tr><tr><td><code>eager</code></td><td>全部</td><td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td><td>No</td></tr></tbody></table> <p>注意事项:</p> <ul><li><code>grouped_mm</code> 专家后端目前仅在通过 <code>torch.compile</code> 编译时支持 <code>bfloat16</code>。此外,它与 CUDA graphs 不兼容,因此编译时必须使用 <code>mode=None</code> 或 <code>mode="max-autotune-no-cudagraphs"</code>。</li> <li><code>eager</code> 专家后端使用数据依赖操作来确定前向传播中使用了哪些专家。该操作与完整图编译(<code>fullgraph=True</code>)不兼容。</li></ul> <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> torch | |
| <span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM | |
| model = AutoModelForCausalLM.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen1.5-MoE-A2.7B"</span>, | |
| dtype=<span class="hljs-string">"bfloat16"</span>, | |
| experts_implementation=<span class="hljs-string">"grouped_mm"</span>, | |
| device_map=<span class="hljs-string">"auto"</span>, | |
| ).<span class="hljs-built_in">eval</span>() | |
| <span class="hljs-comment"># Works for grouped_mm (no CUDA graphs)</span> | |
| model.forward = torch.<span class="hljs-built_in">compile</span>(model.forward, mode=<span class="hljs-string">"max-autotune-no-cudagraphs"</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="benchmarks" 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="#benchmarks"><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>此<a href="https://github.com/user-attachments/files/24125816/bench.py" rel="nofollow">基准测试</a>比较了不同输入大小和专家实现,包括使用和不使用<code>torch.compile</code>的情况。</p> <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/experts_interface.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, 51], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 48.9 kB
- Xet hash:
- 2c3b21453f6abae2d0e6f0bb1f7af752a097b850472395f65bfdfab79f390b5f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.