Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Backends experts","local":"experts-backends","sections":[{"title":"Bascule de l'étape de décodage","local":"decode-stage-switching","sections":[],"depth":2},{"title":"Définir un backend d'experts","local":"set-an-experts-backend","sections":[],"depth":2},{"title":"Backend d'experts spécifiques au backbone","local":"backbone-specific-experts-backend","sections":[],"depth":2},{"title":"DeepGEMM","local":"deepgemm","sections":[{"title":"Experts quantisés en FP8 et FP4","local":"fp8-and-fp4-quantized-experts","sections":[],"depth":3},{"title":"Mega MoE fusionné sur 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/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><!---->Backends experts<!----></span></h1><!--]--><!----> <p>Toutes les implémentations Mixture-of-Experts (MoE) effectuent le même calcul de haut niveau. Pour chaque jeton, un routeur sélectionne <em>k</em> experts. L’état caché du jeton est ensuite projeté à travers les paramètres des experts sélectionnés et agrégé avec les poids de routage. La différence entre les backends d’experts réside dans <em>la manière</em> dont ces multiplications de matrices d’experts sont exécutées.</p> <p><code>ExpertsInterface</code> fournit des backends d’experts optimisés. Il découple l’implémentation des experts du code du modèle afin de simplifier l’expérimentation avec différentes fonctions. Ajoutez de nouveaux backends via la même interface.</p> <table><thead><tr><th>experts backend</th><th>description</th><th>GPU</th><th>CPU</th></tr></thead><tbody><tr><td><code>"eager"</code></td><td>Implémentation de référence qui itère sur les experts sélectionnés et applique des projections sur leurs jetons.</td><td>Des performances de référence raisonnables sans nécessiter de compilation.</td><td>Plus lent que <code>grouped_mm</code> mais plus rapide que <code>batched_mm</code>.</td></tr><tr><td><code>"batched_mm"</code></td><td>Duplique les paramètres d’expert sélectionnés pour chaque jeton et projette tous les jetons dans une seule GEMM par lots à l’aide de <a href="https://docs.pytorch.org/docs/stable/generated/torch.bmm.html" rel="nofollow">torch.bmm</a>.</td><td>Le plus rapide pour les petites entrées, en particulier avec la compilation. Utilise plus de mémoire en raison de la duplication des paramètres.</td><td>Non recommandé (nettement plus lent que les autres backends).</td></tr><tr><td><code>"grouped_mm"</code></td><td>Trie les jetons par les experts sélectionnés et utilise <a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.grouped_mm.html" rel="nofollow">torch.nn.functional.grouped_mm</a> pour projeter tous les jetons en une seule GEMM groupée (nécessite PyTorch 2.9+).</td><td>Idéal pour les entrées de grande taille et plus efficace en mémoire car il évite de dupliquer les paramètres des experts. Rapide avec la compilation.</td><td>Backend le plus efficace pour toutes les tailles d'entrée.</td></tr><tr><td><code>"deepgemm"</code></td><td>Trie les jetons par expert sélectionné et projette tous les jetons dans une seule GEMM groupée alignée sur TMA à l'aide des noyaux <a href="https://github.com/deepseek-ai/DeepGEMM" rel="nofollow">DeepGEMM</a> de <a href="https://huggingface.co/kernels-community/deep-gemm" rel="nofollow">kernels-community/deep-gemm</a>.</td><td>Backend natif pour les modèles DeepSeek sur Hopper (SM90+) et Blackwell (SM100+) ; prend en charge <code>bfloat16</code> et les experts quantifiés en FP8/FP4.</td><td>Non pris en charge (CUDA uniquement).</td></tr><tr><td><code>"deepgemm_megamoe"</code></td><td>Fusionne la distribution parallèle d’experts, le MLP à porte (projection ascendante, SwiGLU, projection descendante) et la combinaison EP en un seul noyau DeepGEMM Mega MoE, en superposant les transferts NVLink au calcul des tensor cores.</td><td>Uniquement sur Blackwell (SM100+), pour les experts quantifiés en FP4 exécutés avec un parallélisme d'experts.</td><td>Non pris en charge (CUDA uniquement).</td></tr><tr><td><code>"sonicmoe"</code></td><td>Fusionne le calcul avant routé <code>bfloat16</code> MoE (dispatch du routeur, projection montée avec porte, activation, projection descendante) dans des noyaux grouped-GEMM CuteDSL (de la bibliothèque <a href="https://github.com/Dao-AILab/quack" rel="nofollow">quack</a>) issus de <a href="https://huggingface.co/kernels-community/sonic-moe" rel="nofollow">kernels-community/sonic-moe</a>.</td><td>Débit de pointe sur Hopper (SM90+) pour les experts <code>bfloat16</code> avec une activation à porte (SwiGLU/GeGLU/ReGLU), en particulier pour l'entraînement.</td><td>Non pris en charge (CUDA uniquement).</td></tr></tbody></table> <p>Les backends <code>"batched_mm"</code> et <code>"grouped_mm"</code> exécutent également les experts quantisés en FP8 et FP4 (<code>int8</code>-packed) via le noyau Triton finegrained-fp8, en lisant des échelles <code>float32</code> ou UE8M0. Ils servent de solution de repli pour les points de contrôle quantisés lorsque le backend <code>"deepgemm"</code> n'est pas disponible.</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><!---->Bascule de l'étape de décodage<!----></span></h2><!--]--><!----> <p>Sur GPU, un modèle chargé avec <code>experts_implementation="grouped_mm"</code> bascule automatiquement sur <code>"batched_mm"</code> pour l’étape de décodage de la génération, ce qui est nettement plus rapide pour de faibles nombres de jetons. L’arrière-plan d’origine est rétabli une fois la génération terminée. Sur CPU, <code>grouped_mm</code> reste actif tout au long de la génération, car il est plus efficace quelle que soit la taille de l’entrée.</p> <p>Le commutateur atteint les couches MoE du modèle de niveau supérieur et de tout backbone de sous-configuration, tel que le <code>text_config</code> d'un modèle vision-langage. Seules les entrées <code>grouped_mm</code> basculent vers <code>batched_mm</code>. Les experts exécutés sur tout autre backend le conservent.</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><!---->Définir un backend d'experts<!----></span></h2><!--]--><!----> <p>Utilisez l'argument <code>experts_implementation</code> dans <a href="/docs/transformers/main/fr/main_classes/model#transformers.PreTrainedModel.from_pretrained">from_pretrained()</a> pour instancier un modèle avec un backend d'experts spécifique.</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> Copié</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>Basculez entre les backends d'experts à l'exécution sans recharger le modèle à l'aide de <a href="/docs/transformers/main/fr/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> Copié</div><!----></button><!----></div> <pre class="language-py "><!---->model.set_experts_implementation(<span class="hljs-string">"eager"</span>)<!----></pre></div><!----> <p>Lisez le backend actuellement en cours d’exécution avec <a href="/docs/transformers/main/fr/main_classes/model#transformers.PreTrainedModel.get_experts_implementation">get_experts_implementation()</a>. Il renvoie un <code>dict</code> avec une entrée pour le modèle et une entrée par sous-configuration.</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> Copié</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><!---->Backend d'experts spécifiques au backbone<!----></span></h2><!--]--><!----> <p>Les modèles multimodaux peuvent avoir plusieurs sous-configurations (par exemple, différents backbones). Vous pouvez définir un backend d'experts différent pour chaque sous-configuration en passant un <code>dict</code> à <code>experts_implementation</code> au moment du chargement.</p> <p>Les clés de la correspondance doivent correspondre aux noms des sous-configurations.</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> Copié</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>Définissez le backend des experts globalement avec une clé vide.</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> Copié</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>Le backend <code>"deepgemm"</code> achemine les produits matriciels d'experts via les noyaux <a href="https://github.com/deepseek-ai/DeepGEMM" rel="nofollow">DeepGEMM</a> distribués par <a href="https://huggingface.co/kernels-community/deep-gemm" rel="nofollow">kernels-community/deep-gemm</a>. Il fonctionne avec des experts non quantifiés en <code>bfloat16</code> et avec des experts quantisés en FP8/FP4 chargés via <a href="./quantization/finegrained_fp8">Fine-grained FP8</a>.</p> <p>Le backend <code>"deepgemm"</code> nécessite :</p> <ul><li>GPU CUDA avec une capacité de calcul ≥ 9.0 (Hopper ou plus récent).</li> <li>Un kit CUDA complet avec <code>nvcc</code> pour la compilation JIT du noyau, version 12.3 ou ultérieure sur Hopper et 12.9 ou ultérieure sur Blackwell. Une installation runtime-only ne contient pas <code>nvcc</code> et est rejetée. Transformers localise le kit via <code>CUDA_HOME</code>, <code>CUDA_PATH</code>, <code>nvcc</code> sur <code>PATH</code>, puis <code>/usr/local/cuda</code>.</li> <li>Le package <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> Copié</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>Le noyau est chargé de manière paresseuse lors du premier passage avant.</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><!---->Experts quantisés en FP8 et FP4<!----></span></h3><!--]--><!----> <p>Les points de contrôle de type DeepSeek sont généralement pré-quantifiés et incluent leur propre configuration de quantification, il n’est donc pas nécessaire de passer un <a href="/docs/transformers/main/fr/main_classes/quantization#transformers.FineGrainedFP8Config">FineGrainedFP8Config</a>. L’arrière-plan <code>"deepgemm"</code> sélectionne automatiquement le noyau de GEMM groupé FP8 (ou FP4 sur Blackwell). DeepGEMM exige des échelles d’activation dynamiques par ligne (<code>activation_scheme="dynamic"</code>) et rejette la quantification d’activation statique (par tenseur).</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> Copié</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>Pour les poids d'experts en FP4 (style DeepSeek V4), le GPU doit être SM100+ (Blackwell). La configuration du checkpoint définit généralement <code>expert_dtype="fp4"</code> et <code>scale_fmt="ue8m0"</code>.</p> <blockquote class="note"><p>Sur Blackwell (SM100+), les noyaux d’experts <code>"deepgemm"</code> et <code>"deepgemm_megamoe"</code> exigent des échelles d’experts UE8M0 qui soient des puissances de deux. Un point de contrôle quantisé avec des échelles <code>float32</code> simples (<code>scale_fmt="float"</code>) lève une <code>ValueError</code> lors de la première passe avant plutôt que de corrompre silencieusement la sortie. Chargez un point de contrôle quantisé avec <code>scale_fmt="ue8m0"</code>, ou passez à <code>grouped_mm</code> ou <code>batched_mm</code>, qui consomment directement les échelles de bloc <code>float32</code>. Hopper (SM90+) consomme des échelles <code>float32</code> sur le chemin DeepGEMM sans conversion.</p></blockquote> <p>La principale raison de passer un <a href="/docs/transformers/main/fr/main_classes/quantization#transformers.FineGrainedFP8Config">FineGrainedFP8Config</a> pour un point de contrôle pré-quantifié est de le déquantifier en <code>bfloat16</code>, auquel cas les experts s'exécutent en <code>bfloat16</code> plutôt que sur le chemin DeepGEMM FP8/FP4.</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> Copié</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><!---->Mega MoE fusionné sur Blackwell<!----></span></h3><!--]--><!----> <p>Sur Blackwell (SM100+), définissez <code>experts_implementation="deepgemm_megamoe"</code> pour exécuter un noyau fusionné unique qui combine la distribution parallèle des experts, la projection ascendante, SwiGLU, la projection descendante et la combinaison EP, en superposant les transferts NVLink au calcul des tensor cores.</p> <p>Ce backend nécessite :</p> <ul><li>Un GPU Blackwell (capacité de calcul ≥ 10.0) avec un kit CUDA (<code>nvcc</code>) 12.9 ou ultérieur.</li> <li>Poids d'experts en FP4 appariés avec des échelles de poids UE8M0 (le point de contrôle pré-quantifié déclare généralement <code>expert_dtype="fp4"</code> et <code>scale_fmt="ue8m0"</code> dans sa configuration).</li> <li>Un groupe de processus <code>torch.distributed</code> pour le groupe parallèle d'experts, fourni automatiquement par l'habillage parallèle d'experts lorsque <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> Copié</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>Le backend <code>"sonicmoe"</code> fusionne le calcul avant routé MoE (dispatch, projection gated up, activation, projection down) dans un ensemble de noyaux CuteDSL grouped-GEMM hautement optimisés, construits sur la bibliothèque <a href="https://github.com/Dao-AILab/quack" rel="nofollow">quack</a> et distribués par <a href="https://huggingface.co/kernels-community/sonic-moe" rel="nofollow">kernels-community/sonic-moe</a>.</p> <p>Le backend <code>"sonicmoe"</code> nécessite :</p> <ul><li>GPU CUDA avec une capacité de calcul ≥ 9.0 (Hopper ou plus récent).</li> <li>Le package <a href="https://github.com/huggingface/kernels" rel="nofollow">kernels</a> et le package <code>nvidia-cutlass-dsl</code>.</li> <li>Experts avec une activation à porte (<code>silu</code>, <code>gelu</code> ou <code>relu</code>, mappée sur 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> Copié</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>Si les exigences ne sont pas remplies, la méthode forward lève <code>ImportError</code> et vous devez choisir une autre valeur pour <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>Les backends <code>"eager"</code>, <code>"batched_mm"</code> et <code>"grouped_mm"</code> sont compatibles avec <code>torch.compile</code> dans des proportions variables. Le tableau suivant résume leur compatibilité. Les backends <code>"deepgemm"</code>, <code>"deepgemm_megamoe"</code> et <code>"sonicmoe"</code> passent par des noyaux CUDA externes et ne sont pas couverts par ce tableau.</p> <table><thead><tr><th>Implémentation</th><th>modes de compilation</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>Oui</td></tr><tr><td><code>grouped_mm</code> (repli)</td><td><code>None</code>, <code>max-autotune-no-cudagraphs</code></td><td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td><td>Oui</td></tr><tr><td><code>batched_mm</code></td><td>tous</td><td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td><td>Oui</td></tr><tr><td><code>eager</code></td><td>tous</td><td><code>bfloat16</code>, <code>float16</code>, <code>float32</code></td><td>Non</td></tr></tbody></table> <p>Remarques :</p> <ul><li>Le backend d'experts <code>grouped_mm</code> prend actuellement en charge uniquement <code>bfloat16</code> lors de la compilation avec <code>torch.compile</code>. De plus, il n'est pas compatible avec les graphes CUDA, il faut donc utiliser <code>mode=None</code> ou <code>mode="max-autotune-no-cudagraphs"</code> lors de la compilation.</li> <li>Le backend d'experts <code>eager</code> utilise une opération dépendante des données pour déterminer quels experts sont utilisés lors d'un passage avant. Cette opération n'est pas compatible avec la compilation complète du graphe (<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> Copié</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><!---->Benchmarks<!----></span></h2><!--]--><!----> <p>Ce <a href="https://github.com/user-attachments/files/24125816/bench.py" rel="nofollow">benchmark</a> compare différentes tailles d'entrée et implémentations d'experts, avec et sans <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:
- 50.6 kB
- Xet hash:
- 7a0b9e4dc6a29f197020aff6fd4d70c5c114df900ca08ce90c3607d5c28a6394
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.