Buckets:

hf-doc-build/doc-dev / transformers /pr_41992 /en /experts_interface.html
download
raw
46.5 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Experts backends&quot;,&quot;local&quot;:&quot;experts-backends&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Set an experts backend&quot;,&quot;local&quot;:&quot;set-an-experts-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Backbone-specific experts backend&quot;,&quot;local&quot;:&quot;backbone-specific-experts-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;DeepGEMM&quot;,&quot;local&quot;:&quot;deepgemm&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;FP8 and FP4 quantized experts&quot;,&quot;local&quot;:&quot;fp8-and-fp4-quantized-experts&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Fused Mega MoE on Blackwell&quot;,&quot;local&quot;:&quot;fused-mega-moe-on-blackwell&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SonicMoE&quot;,&quot;local&quot;:&quot;sonicmoe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;torch.compile&quot;,&quot;local&quot;:&quot;torchcompile&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;local&quot;:&quot;benchmarks&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/transformers/pr_41992/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/entry/start.6ac19d43.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/scheduler.0dad8db1.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/singletons.9505f7e3.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/index.595571e0.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/paths.a2014728.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/entry/app.42da82bd.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/preload-helper.43417560.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/index.855abf52.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/nodes/0.dc15b4bc.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/nodes/47.34c9df51.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/CopyLLMTxtMenu.9e1f0f49.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.bf05d4d8.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/IconCopy.ffdeebb6.js">
<link rel="modulepreload" href="/docs/transformers/pr_41992/en/_app/immutable/chunks/CodeBlock.b28b91fa.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Experts backends&quot;,&quot;local&quot;:&quot;experts-backends&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Set an experts backend&quot;,&quot;local&quot;:&quot;set-an-experts-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Backbone-specific experts backend&quot;,&quot;local&quot;:&quot;backbone-specific-experts-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;DeepGEMM&quot;,&quot;local&quot;:&quot;deepgemm&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;FP8 and FP4 quantized experts&quot;,&quot;local&quot;:&quot;fp8-and-fp4-quantized-experts&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Fused Mega MoE on Blackwell&quot;,&quot;local&quot;:&quot;fused-mega-moe-on-blackwell&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SonicMoE&quot;,&quot;local&quot;:&quot;sonicmoe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;torch.compile&quot;,&quot;local&quot;:&quot;torchcompile&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Benchmarks&quot;,&quot;local&quot;:&quot;benchmarks&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div 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>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> </div> <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 class="" 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 backends</span></h1> <p data-svelte-h="svelte-126nyrc">All Mixture-of-Experts (MoE) implementations perform the same high-level computation. For each token, a router selects <em>k</em> experts. The token hidden state is then projected through the selected experts’ parameters and aggregated with routing weights. The difference between experts backends is <em>how</em> those expert matrix multiplications execute.</p> <p data-svelte-h="svelte-12gq5xs">The <code>ExpertsInterface</code> provides optimized experts backends. It decouples the experts implementation from the model code to simplify experimentation with different functions. Add new backends through the same interface.</p> <table data-svelte-h="svelte-1nkn2c8"><thead><tr><th>experts backend</th> <th>description</th> <th>GPU</th> <th>CPU</th></tr></thead> <tbody><tr><td><code>&quot;eager&quot;</code></td> <td>Reference implementation that loops over selected experts and applies projections on their tokens.</td> <td>Reasonable baseline performance without requiring compilation.</td> <td>Slower than <code>grouped_mm</code> but faster than <code>batched_mm</code>.</td></tr> <tr><td><code>&quot;batched_mm&quot;</code></td> <td>Duplicates selected expert parameters for each token and projects all tokens in a single batched GEMM using <a href="https://docs.pytorch.org/docs/stable/generated/torch.bmm.html" rel="nofollow">torch.bmm</a>.</td> <td>Fastest for small inputs, especially with compilation. Uses more memory due to parameter duplication.</td> <td>Not recommended (significantly slower than other backends).</td></tr> <tr><td><code>&quot;grouped_mm&quot;</code></td> <td>Orders tokens by selected experts and uses <a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.grouped_mm.html" rel="nofollow">torch.nn.functional.grouped_mm</a> to project all tokens in a single grouped GEMM (requires PyTorch 2.9+).</td> <td>Best for larger inputs and more memory efficient as it avoids duplicating expert parameters. Fast with compilation.</td> <td>Most efficient backend for all input sizes.</td></tr> <tr><td><code>&quot;deepgemm&quot;</code></td> <td>Sorts tokens by selected expert and projects all tokens in a single TMA-aligned grouped GEMM using the <a href="https://github.com/deepseek-ai/DeepGEMM" rel="nofollow">DeepGEMM</a> kernels from <a href="https://huggingface.co/kernels-community/deep-gemm" rel="nofollow">kernels-community/deep-gemm</a>.</td> <td>Native backend for DeepSeek models on Hopper (SM90+) and Blackwell (SM100+); supports <code>bfloat16</code> and FP8/FP4-quantized experts.</td> <td>Not supported (CUDA-only).</td></tr> <tr><td><code>&quot;deepgemm_megamoe&quot;</code></td> <td>Fuses expert-parallel dispatch, the gated MLP (up projection, SwiGLU, down projection), and the EP combine into a single DeepGEMM Mega MoE kernel, overlapping NVLink transfers with tensor-core compute.</td> <td>Blackwell (SM100+) only, for FP4-quantized experts run with expert parallelism.</td> <td>Not supported (CUDA-only).</td></tr> <tr><td><code>&quot;sonicmoe&quot;</code></td> <td>Fuses the routed <code>bfloat16</code> MoE forward (router dispatch, gated up projection, activation, down projection) into CuteDSL grouped-GEMM kernels (from the <a href="https://github.com/Dao-AILab/quack" rel="nofollow">quack</a> library) from <a href="https://huggingface.co/kernels-community/sonic-moe" rel="nofollow">kernels-community/sonic-moe</a>.</td> <td>State-of-the-art throughput on Hopper (SM90+) for <code>bfloat16</code> experts with a gated activation (SwiGLU/GeGLU/ReGLU), especially for training.</td> <td>Not supported (CUDA-only).</td></tr></tbody></table> <p data-svelte-h="svelte-djn9sn">The <code>&quot;batched_mm&quot;</code> and <code>&quot;grouped_mm&quot;</code> backends also run FP8 and FP4 (<code>int8</code>-packed) quantized experts through the Triton finegrained-fp8 kernel, reading either <code>float32</code> or UE8M0 scales. They act as the fallback for quantized checkpoints when the <code>&quot;deepgemm&quot;</code> backend is unavailable.</p> <blockquote class="note" data-svelte-h="svelte-11kzsgw"><p>When using <code>experts_implementation=&quot;grouped_mm&quot;</code> on GPU, the model automatically switches to <code>&quot;batched_mm&quot;</code> during the decode stage of generation (after prefill). This is because <code>batched_mm</code> is significantly faster on lower token count during autoregressive decoding on GPU. On CPU, <code>grouped_mm</code> remains active throughout generation as it is more efficient for all input sizes.</p></blockquote> <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 class="" 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>Set an experts backend</span></h2> <p data-svelte-h="svelte-l5kvi7">Use the <code>experts_implementation</code> argument in <a href="/docs/transformers/pr_41992/en/main_classes/model#transformers.PreTrainedModel.from_pretrained">from_pretrained()</a> to instantiate a model with a specific experts backend.</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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen1.5-MoE-A2.7B&quot;</span>,
dtype=<span class="hljs-string">&quot;bfloat16&quot;</span>,
experts_implementation=<span class="hljs-string">&quot;batched_mm&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1uio7af">Switch between experts backends at runtime without reloading the model using <a href="/docs/transformers/pr_41992/en/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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START -->model.set_experts_implementation(<span class="hljs-string">&quot;eager&quot;</span>)<!-- HTML_TAG_END --></pre></div> <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 class="" 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>Backbone-specific experts backend</span></h2> <p data-svelte-h="svelte-17bh5xl">Multimodal models can have multiple sub-configs (for example, different backbones). You can set a different experts backend per sub-config by passing a <code>dict</code> to <code>experts_implementation</code> at load time.</p> <p data-svelte-h="svelte-pdltj3">Keys in the mapping must match sub-config names.</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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForImageTextToText
experts_implementation_per_backbone = {
<span class="hljs-string">&quot;text_config&quot;</span>: <span class="hljs-string">&quot;grouped_mm&quot;</span>,
<span class="hljs-string">&quot;vision_config&quot;</span>: <span class="hljs-string">&quot;eager&quot;</span>,
}
model = AutoModelForImageTextToText.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen3-VL-Moe&quot;</span>,
experts_implementation=experts_implementation_per_backbone,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-5rug0z">Set the experts backend globally with an empty key.</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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START -->model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen1.5-MoE-A2.7B&quot;</span>,
experts_implementation={<span class="hljs-string">&quot;&quot;</span>: <span class="hljs-string">&quot;batched_mm&quot;</span>},
)<!-- HTML_TAG_END --></pre></div> <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 class="" 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 data-svelte-h="svelte-m9tdaa">The <code>&quot;deepgemm&quot;</code> backend routes expert matmuls through the <a href="https://github.com/deepseek-ai/DeepGEMM" rel="nofollow">DeepGEMM</a> kernels distributed by <a href="https://huggingface.co/kernels-community/deep-gemm" rel="nofollow">kernels-community/deep-gemm</a>. It works with unquantized <code>bfloat16</code> experts and with FP8/FP4-quantized experts loaded through <a href="./quantization/finegrained_fp8">Fine-grained FP8</a>.</p> <p data-svelte-h="svelte-46wytg">The <code>&quot;deepgemm&quot;</code> backend requires:</p> <ul data-svelte-h="svelte-eiks5j"><li>CUDA GPU with compute capability ≥ 9.0 (Hopper or newer).</li> <li>CUDA runtime 12.3 or later on Hopper, 12.9 or later on Blackwell.</li> <li><code>nvcc</code>/<code>nvrtc</code> available on the system for the kernel’s JIT compilation.</li> <li>The <a href="https://github.com/huggingface/kernels" rel="nofollow">kernels</a> package.</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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;deepseek-ai/DeepSeek-V3&quot;</span>,
dtype=<span class="hljs-string">&quot;bfloat16&quot;</span>,
experts_implementation=<span class="hljs-string">&quot;deepgemm&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1qb1xsz">The kernel is loaded lazily on the first forward.</p> <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 class="" 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 and FP4 quantized experts</span></h3> <p data-svelte-h="svelte-1x7spqr">DeepSeek-style checkpoints are usually pre-quantized and carry their own quantization config, so you don’t need to pass a <a href="/docs/transformers/pr_41992/en/main_classes/quantization#transformers.FineGrainedFP8Config">FineGrainedFP8Config</a>. The <code>&quot;deepgemm&quot;</code> backend automatically picks the FP8 (or FP4 on Blackwell) grouped-GEMM kernel. DeepGEMM requires dynamic per-row activation scales (<code>activation_scheme=&quot;dynamic&quot;</code>) and rejects static (per-tensor) activation quantization.</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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;deepseek-ai/DeepSeek-V3&quot;</span>,
experts_implementation=<span class="hljs-string">&quot;deepgemm&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-6gfsqn">For FP4-packed expert weights (DeepSeek V4-style), the GPU must be SM100+ (Blackwell). The checkpoint config typically sets <code>expert_dtype=&quot;fp4&quot;</code> and <code>scale_fmt=&quot;ue8m0&quot;</code>.</p> <blockquote class="note" data-svelte-h="svelte-vrg2sw"><p>On Blackwell (SM100+), the <code>&quot;deepgemm&quot;</code> and <code>&quot;deepgemm_megamoe&quot;</code> experts kernels require power-of-two UE8M0 expert scales. A checkpoint quantized with plain <code>float32</code> scales (<code>scale_fmt=&quot;float&quot;</code>) raises a <code>ValueError</code> on the first forward instead of silently corrupting the output. Load a checkpoint quantized with <code>scale_fmt=&quot;ue8m0&quot;</code>, or switch to <code>grouped_mm</code> or <code>batched_mm</code>, which consume <code>float32</code> block scales directly. Hopper (SM90+) consumes <code>float32</code> scales on the DeepGEMM path without conversion.</p></blockquote> <p data-svelte-h="svelte-zguxse">The main reason to pass a <a href="/docs/transformers/pr_41992/en/main_classes/quantization#transformers.FineGrainedFP8Config">FineGrainedFP8Config</a> for a pre-quantized checkpoint is to dequantize it back to <code>bfloat16</code>, in which case the experts run in <code>bfloat16</code> rather than on the FP8/FP4 DeepGEMM path.</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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, FineGrainedFP8Config
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;deepseek-ai/DeepSeek-V3&quot;</span>,
quantization_config=FineGrainedFP8Config(dequantize=<span class="hljs-literal">True</span>),
experts_implementation=<span class="hljs-string">&quot;deepgemm&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <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 class="" 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>Fused Mega MoE on Blackwell</span></h3> <p data-svelte-h="svelte-ns4r2o">On Blackwell (SM100+), set <code>experts_implementation=&quot;deepgemm_megamoe&quot;</code> to run a single fused kernel that combines expert-parallel dispatch, the up projection, SwiGLU, the down projection, and the EP combine, overlapping NVLink transfers with tensor-core compute.</p> <p data-svelte-h="svelte-1sp62g6">This backend requires:</p> <ul data-svelte-h="svelte-zt9dcu"><li>A Blackwell GPU (compute capability ≥ 10.0) with CUDA runtime 12.9 or later.</li> <li>FP4-packed expert weights paired with UE8M0 weight scales (the pre-quantized checkpoint typically declares <code>expert_dtype=&quot;fp4&quot;</code> and <code>scale_fmt=&quot;ue8m0&quot;</code> in its config).</li> <li>A <code>torch.distributed</code> process group for the expert-parallel group, which the tensor-parallel wrapping supplies automatically.</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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;deepseek-ai/DeepSeek-V4&quot;</span>,
experts_implementation=<span class="hljs-string">&quot;deepgemm_megamoe&quot;</span>,
tp_plan=<span class="hljs-string">&quot;auto&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <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 class="" 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 data-svelte-h="svelte-1kgu5ho">The <code>&quot;sonicmoe&quot;</code> backend fuses the routed MoE forward (dispatch, gated up projection, activation, down projection) into a set of highly optimized CuteDSL grouped-GEMM kernels, built on the <a href="https://github.com/Dao-AILab/quack" rel="nofollow">quack</a> library and distributed by <a href="https://huggingface.co/kernels-community/sonic-moe" rel="nofollow">kernels-community/sonic-moe</a>.</p> <p data-svelte-h="svelte-161a4db">The <code>&quot;sonicmoe&quot;</code> backend requires:</p> <ul data-svelte-h="svelte-16aw06b"><li>CUDA GPU with compute capability ≥ 9.0 (Hopper or newer).</li> <li>The <a href="https://github.com/huggingface/kernels" rel="nofollow">kernels</a> package and the <code>nvidia-cutlass-dsl</code> package.</li> <li>Experts with a gated activation (<code>silu</code>, <code>gelu</code>, or <code>relu</code>, mapped to 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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;Qwen/Qwen1.5-MoE-A2.7B&quot;</span>,
dtype=<span class="hljs-string">&quot;bfloat16&quot;</span>,
experts_implementation=<span class="hljs-string">&quot;sonicmoe&quot;</span>,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-305jrf">If the requirements aren’t met, the forward raises <code>ImportError</code> and you should pick a different <code>experts_implementation</code>.</p> <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 class="" 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 data-svelte-h="svelte-18mwx21">The <code>&quot;eager&quot;</code>, <code>&quot;batched_mm&quot;</code>, and <code>&quot;grouped_mm&quot;</code> backends are compatible with <code>torch.compile</code> to varying degrees. The following table summarizes their compatibility. The <code>&quot;deepgemm&quot;</code>, <code>&quot;deepgemm_megamoe&quot;</code>, and <code>&quot;sonicmoe&quot;</code> backends route through external CUDA kernels and aren’t covered by this table.</p> <table data-svelte-h="svelte-18ojk73"><thead><tr><th>Implementation</th> <th>compilation modes</th> <th>dtypes</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>Yes</td></tr> <tr><td><code>grouped_mm</code> (fallback)</td> <td><code>None</code>, <code>max-autotune-no-cudagraphs</code></td> <td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td> <td>Yes</td></tr> <tr><td><code>batched_mm</code></td> <td>all</td> <td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td> <td>Yes</td></tr> <tr><td><code>eager</code></td> <td>all</td> <td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td> <td>No</td></tr></tbody></table> <p data-svelte-h="svelte-1biq3pv">Notes:</p> <ul data-svelte-h="svelte-1hsback"><li>The <code>grouped_mm</code> experts backend currently only supports <code>bfloat16</code> when compiled with <code>torch.compile</code>. Additionally, it is not compatible with CUDA graphs, so you must use <code>mode=None</code> or <code>mode=&quot;max-autotune-no-cudagraphs&quot;</code> when compiling.</li> <li>The <code>eager</code> experts backend uses a data-dependent operation to find which experts are used in a forward pass. This operation is not compatible with full graph compilation (<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 class="" 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> Copied</div></button></div> <pre class="language-py "><!-- HTML_TAG_START --><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">&quot;Qwen/Qwen1.5-MoE-A2.7B&quot;</span>,
dtype=<span class="hljs-string">&quot;bfloat16&quot;</span>,
experts_implementation=<span class="hljs-string">&quot;grouped_mm&quot;</span>,
).<span class="hljs-built_in">eval</span>().cuda()
<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">&quot;max-autotune-no-cudagraphs&quot;</span>)<!-- HTML_TAG_END --></pre></div> <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 class="" 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>Benchmarks</span></h2> <p data-svelte-h="svelte-1c6q5vt">This <a href="https://github.com/user-attachments/files/24125816/bench.py" rel="nofollow">benchmark</a> compares different input sizes and experts implementations with and without <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 data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_1cvdjju = {
assets: "/docs/transformers/pr_41992/en",
base: "/docs/transformers/pr_41992/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/transformers/pr_41992/en/_app/immutable/entry/start.6ac19d43.js"),
import("/docs/transformers/pr_41992/en/_app/immutable/entry/app.42da82bd.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 47],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
46.5 kB
·
Xet hash:
477226ff2fd0b4505cd692850be36eb89690ce9491807c30915ce7810e13f9eb

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