Buckets:

hf-doc-build/doc / transformers /main /id /expert_parallelism.html
mishig's picture
download
raw
79.2 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Paralelisme pakar&quot;,&quot;local&quot;:&quot;expert-parallelism&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DistributedConfig&quot;,&quot;local&quot;:&quot;distributedconfig&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Memilih implementasi&quot;,&quot;local&quot;:&quot;choosing-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Menambahkan implementasi&quot;,&quot;local&quot;:&quot;adding-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Mengganti rencana&quot;,&quot;local&quot;:&quot;overriding-the-plans&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Token dispatch&quot;,&quot;local&quot;:&quot;token-dispatch&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Bagaimana ukuran-ukuran tersebut digabungkan&quot;,&quot;local&quot;:&quot;how-the-sizes-combine&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Dengan paralelisme tensor&quot;,&quot;local&quot;:&quot;with-tensor-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Menggabungkan dengan FSDP2&quot;,&quot;local&quot;:&quot;combining-with-fsdp2&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Referensi API&quot;,&quot;local&quot;:&quot;transformers.DistributedConfig&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DS27pc2p.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Bbt6a5UJ.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DGxd7hEC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/hD794eHD.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DG_DvbXP.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/0.Dperbprz.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DIDdACb8.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/5.jXI389aE.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CBFQlbiL.js" rel="modulepreload">
<!--9m8k7m--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Expert parallelism&quot;,&quot;local&quot;:&quot;expert-parallelism&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DistributedConfig&quot;,&quot;local&quot;:&quot;distributedconfig&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Choosing an implementation&quot;,&quot;local&quot;:&quot;choosing-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Adding an implementation&quot;,&quot;local&quot;:&quot;adding-an-implementation&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Overriding the plans&quot;,&quot;local&quot;:&quot;overriding-the-plans&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Token dispatch&quot;,&quot;local&quot;:&quot;token-dispatch&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;How the sizes combine&quot;,&quot;local&quot;:&quot;how-the-sizes-combine&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;With tensor parallelism&quot;,&quot;local&quot;:&quot;with-tensor-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Combining with FSDP2&quot;,&quot;local&quot;:&quot;combining-with-fsdp2&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;API reference&quot;,&quot;local&quot;:&quot;transformers.DistributedConfig&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="expert-parallelism" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#expert-parallelism"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Paralelisme pakar<!----></span></h1><!--]--><!----> <p><a href="https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=expert_parallelism" rel="nofollow">Expert parallelism</a> adalah strategi paralelisasi untuk <a href="https://huggingface.co/blog/moe" rel="nofollow">model mixture-of-experts (MoE)</a>. Lapisan feedforward dari setiap expert berada di akselerator perangkat keras yang berbeda. Router meneruskan token ke expert yang sesuai dan mengumpulkan hasilnya. Pendekatan ini memungkinkan model diskalakan ke jumlah parameter yang jauh lebih besar tanpa meningkatkan biaya komputasi, karena setiap token hanya mengaktifkan beberapa expert.</p> <!--[1--><h2 class="relative group"><a id="distributedconfig" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#distributedconfig"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->DistributedConfig<!----></span></h2><!--]--><!----> <p>Aktifkan paralelisme pakar dengan kelas <a href="/docs/transformers/main/id/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a> dan argumen <code>ep_size</code>. Model MoE memecah pakarnya dengan <code>"ep_dispatch_experts"</code>. Implementasinya menentukan bagaimana token mencapai para pakar. <a href="#token-dispatch">Token dispatch</a> memungkinkan <code>ep_size</code> berbeda dari <code>tp_size</code>. Beberapa implementasi EP mengharuskan <code>ep_size=tp_size</code>, misalnya <code>"all_reduce"</code> dan <code>"deepgemm_megamoe"</code>, sehingga setiap rank dalam grup pakar memegang token yang sama.</p> <p>Llama 4 tidak memiliki rencana EP: para ekspertnya mengambil setiap token yang sudah diskalakan oleh skor router-nya, sehingga tidak ada yang perlu didispatch. Pecah mereka dengan tensor parallelism (<code>tp_size</code>) sebagai gantinya.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer
<span class="hljs-keyword">from</span> transformers.distributed.configuration_utils <span class="hljs-keyword">import</span> DistributedConfig
distributed_config = DistributedConfig(
tp_size=<span class="hljs-built_in">int</span>(os.environ[<span class="hljs-string">&quot;WORLD_SIZE&quot;</span>]),
ep_size=<span class="hljs-built_in">int</span>(os.environ[<span class="hljs-string">&quot;WORLD_SIZE&quot;</span>]),
)
model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-string">&quot;openai/gpt-oss-120b&quot;</span>,
distributed_config=distributed_config,
)<!----></pre></div><!----> <p>Setiap model MoE mendefinisikan dua rencana dalam konfigurasinya: <code>base_model_tp_plan</code> untuk modul padat dan <code>base_model_ep_plan</code> untuk para ahli. Keduanya dipaparkan pada model yang dimuat sebagai <code>model.tp_plan</code> dan <code>model.ep_plan</code>. Dengan <code>tp_size > 1</code> dan <code>ep_size > 1</code>, keduanya berlaku: rencana <a href="./perf_infer_gpu_multi">tensor parallel</a> memecah perhatian dan MLP padat, sedangkan rencana expert parallel memecah para ahli. Aturan EP memiliki prioritas lebih tinggi daripada aturan TP untuk modul yang sama, sehingga bobot ahli hanya dipecah sekali, oleh rencana EP. Dalam rencana EP, gaya <code>GroupedGemmParallel</code> membagi bobot ahli sepanjang dimensi ahli sehingga setiap rank hanya memuat ahli lokalnya, dan aturan <code>"ep_dispatch_experts"</code> pada modul ahli menentukan bagaimana token mencapai mereka.</p> <p><code>tp_plan</code> hanya diterapkan ketika <code>tp_size > 1</code>, dan <code>ep_plan</code> hanya ketika <code>ep_size > 1</code>. Dengan TP diaktifkan dan EP dinonaktifkan, rencana TP penuh diterapkan, termasuk aturan expert.</p> <p><code>"ep_dispatch_experts"</code> membagi seluruh ahli per rank (<code>ep_size</code> adalah kelipatan dari <code>tp_size</code> yang membagi <code>fsdp_size * tp_size</code>) dan mengaksesnya melalui implementasi yang dinamakan oleh <code>ep_implementation</code>, lihat <a href="#choosing-an-implementation">Memilih implementasi</a>.</p> <!--[2--><h3 class="relative group"><a id="choosing-an-implementation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#choosing-an-implementation"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Memilih implementasi<!----></span></h3><!--]--><!----> <p><code>"ep_dispatch_experts"</code> memecah para ahli sekali dan memilih cara token mencapai mereka dari implementasi yang terdaftar di <code>EPImplementationInterface</code>, yang dipilih dengan <code>ep_implementation</code> di <a href="/docs/transformers/main/id/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a>:</p> <table><thead><tr><th align="left"><code>ep_implementation</code></th><th align="left">keuntungan</th><th align="left">kekurangan</th></tr></thead><tbody><tr><td align="left"><code>"all_to_all"</code>, lihat <a href="#token-dispatch">penyaluran token</a></td><td align="left">Berfungsi untuk tata letak apa pun, termasuk <code>ep_size > tp_size</code>. Hanya token yang di-rutekan yang berpindah.</td><td align="left">Dua kolektif all-to-all dan satu sinkronisasi host per lapisan, sehingga tidak dapat ditangkap dalam grafik CUDA.</td></tr><tr><td align="left"><code>"all_reduce"</code></td><td align="left">Tidak ada token yang berpindah dan tidak ada sinkronisasi host, sehingga decode dapat ditangkap dalam grafik CUDA.</td><td align="left">Memerlukan <code>ep_size=tp_size</code>. Setiap rank menjalankan expert-nya di seluruh batch, sehingga biayanya meningkat seiring dengan batch.</td></tr><tr><td align="left"><code>"deepgemm_megamoe"</code></td><td align="left">DeepGEMM Mega MoE menggabungkan dispatch, para expert, dan combine dalam satu kernel. Setiap layer dalam grup EP berbagi buffer memori simetrisnya.</td><td align="left">Memerlukan <code>ep_size=tp_size</code>, dan aktivasi FP8 dengan pakar FP4 pada SM100+. Hanya untuk inferensi.</td></tr><tr><td align="left"><code>"auto"</code>, nilai default</td><td align="left">Menjalankan <code>"all_reduce"</code> saat inferensi ketika rank dari grup ahli berbagi satu batch, dan <code>"all_to_all"</code> selain itu.</td><td align="left">Tidak selalu menjadi pilihan tercepat: beban kerja dapat diuntungkan dari implementasi kustom, lihat <a href="#adding-an-implementation">Menambahkan implementasi</a>.</td></tr></tbody></table> <blockquote class="note"><p><code>ep_implementation</code> hanya memilih di antara ukuran paralel yang diatur dalam <a href="/docs/transformers/main/id/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a>. Ketika rank dari sebuah grup ahli memegang batch yang berbeda, misalnya <code>tp_size=1</code> dengan <code>fsdp_size=ep_size</code>, <code>"all_to_all"</code> adalah satu-satunya implementasi yang berlaku.</p></blockquote> <!--[2--><h3 class="relative group"><a id="adding-an-implementation" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#adding-an-implementation"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Menambahkan implementasi<!----></span></h3><!--]--><!----> <p>Implementasi mewarisi <code>EPImplementation</code> dan terdaftar di bawah nama yang dipilih oleh <code>ep_implementation</code>. Ia dibangun sekali per modul pakar, sehingga dapat menyimpan state seperti buffer komunikasi. Memanggilnya dengan batch yang dipegang oleh rank ini, beserta id pakar global, mengembalikan output pakar berbobot untuk masing-masing tokennya. Ia menjalankan pakar milik rank ini dengan <code>experts_forward</code>, pada baris, id pakar lokal, dan bobot yang dibawanya.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers.distributed.expert_parallel <span class="hljs-keyword">import</span> EPImplementation, EPImplementationInterface
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyEP</span>(<span class="hljs-title class_ inherited__">EPImplementation</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__call__</span>(<span class="hljs-params">self, hidden_states, top_k_index, top_k_weights</span>):
tokens, expert_ids, weights = ... <span class="hljs-comment"># bring this rank&#x27;s experts the rows routed to them</span>
expert_output = <span class="hljs-variable language_">self</span>.experts_forward(tokens, expert_ids, weights)
<span class="hljs-keyword">return</span> ... <span class="hljs-comment"># the weighted expert output for each token of the batch this rank holds</span>
EPImplementationInterface.register(<span class="hljs-string">&quot;my_ep&quot;</span>, MyEP)
distributed_config = DistributedConfig(tp_size=<span class="hljs-number">1</span>, fsdp_size=<span class="hljs-number">8</span>, ep_size=<span class="hljs-number">8</span>, ep_implementation=<span class="hljs-string">&quot;my_ep&quot;</span>)<!----></pre></div><!----> <blockquote class="note"><p>Gradien harus mencapai setiap rank yang memegang token, termasuk bobot routing, sehingga bungkus kolektif yang tidak didiferensiasi oleh autograd dalam <code>torch.autograd.Function</code>.</p></blockquote> <blockquote class="tip"><p><code>enable_expert_parallel=True</code> tanpa <code>ep_size</code> sudah tidak didukung dan mengeluarkan <code>FutureWarning</code>. Ini hanya memecah para ahli, seperti <code>ep_size=tp_size, tp_plan={}</code>.</p></blockquote> <p>Jalankan skrip inferensi Anda dengan <a href="https://pytorch.org/docs/stable/elastic/run.html" rel="nofollow">torchrun</a>. Jumlah proses harus sama dengan <code>tp_size * fsdp_size * pp_size</code>, dan <code>ep_size</code> harus membagi jumlah pakar secara merata.</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> Disalin</div><!----></button><!----></div> <pre class="language-zsh "><!---->torchrun --nproc-per-node 8 your_script.py<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="overriding-the-plans" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#overriding-the-plans"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Mengganti rencana<!----></span></h3><!--]--><!----> <p>Lewati <code>tp_plan={...}</code> atau <code>ep_plan={...}</code> ke <a href="/docs/transformers/main/id/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a> untuk menimpa aturan individual dari rencana yang telah ditentukan sebelumnya. Aturan yang tidak ditentukan akan dipertahankan, dan rencana yang digabungkan disimpan pada model. Setiap kunci harus cocok dengan modul, parameter, atau entri rencana yang ada; jika tidak, pemuatan akan memicu <code>ValueError</code> sebelum apa pun dipecah. Gunakan jalur lengkap sebagaimana dilihat dari model yang dimuat, sehingga <code>model.layers.*</code> untuk LM kausal dan <code>layers.*</code> untuk model dasarnya.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!---->distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">4</span>,
ep_size=<span class="hljs-number">4</span>,
tp_plan={<span class="hljs-string">&quot;model.layers.*.self_attn.q_proj&quot;</span>: <span class="hljs-string">&quot;colwise_rep&quot;</span>},
ep_plan={<span class="hljs-string">&quot;model.layers.*.mlp.experts.down_proj&quot;</span>: <span class="hljs-string">&quot;grouped_gemm&quot;</span>},
)<!----></pre></div><!----> <p>Menyediakan rencana tidak menyimpulkan ukuran paralel: atur <code>tp_size</code> dan <code>ep_size</code> secara eksplisit.</p> <p>Dengan <code>ep_size=tp_size</code>, implementasi default <code>"auto"</code> menjalankan <code>"all_reduce"</code> saat inferensi dan mendistribusikan token saat pelatihan. Untuk melakukan masking dan all-reduce juga saat pelatihan, atur <code>ep_implementation="all_reduce"</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Disalin</div><!----></button><!----></div> <pre class="language-py "><!---->distributed_config = DistributedConfig(tp_size=<span class="hljs-number">4</span>, ep_size=<span class="hljs-number">4</span>, ep_implementation=<span class="hljs-string">&quot;all_reduce&quot;</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="token-dispatch" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#token-dispatch"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Token dispatch<!----></span></h2><!--]--><!----> <p>Token dispatch adalah implementasi <code>"all_to_all"</code>: setiap rank melatih pada bagian batch-nya sendiri. Di setiap lapisan MoE, sebuah rank merutekan tokennya, mengirim setiap pasangan (token, expert) ke rank yang memiliki expert tersebut dengan all-to-all, menjalankan expert lokalnya pada apa yang diterimanya, mendapatkan hasilnya kembali dengan all-to-all kedua, dan menggabungkannya dengan bobot routing. Hanya aktivasi yang dirutekan dan output expert yang berpindah, dan tidak ada rank yang menghitung expert untuk token yang tidak dimilikinya.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
<span class="hljs-keyword">from</span> transformers.distributed <span class="hljs-keyword">import</span> DistributedConfig
distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">1</span>,
fsdp_size=<span class="hljs-number">8</span>,
ep_size=<span class="hljs-number">4</span>,
)
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen3-30B-A3B&quot;</span>, distributed_config=distributed_config)<!----></pre></div><!----> <p>Dengan <code>tp_size=1</code>, <code>ep_size</code> harus membagi habis <code>fsdp_size</code> dan jumlah pakar, dan perhatian tidak dibatasi oleh <code>num_key_value_heads</code>. Untuk sisa model:</p> <ul><li>Parameter di luar para ahli dipecah dengan <a href="./fsdp">FSDP2</a> di sepanjang <code>fsdp</code>, yang mengurangi gradien mereka.</li> <li>Para ahli di-shard di <code>ep</code> dan, ketika <code>efsdp_size = fsdp_size * tp_size // ep_size</code> lebih besar dari satu, juga di-shard FSDP di <code>efsdp</code>. Mereka selalu dibungkus FSDP, sehingga <code>fsdp_mixed_precision</code> dan <code>fsdp_cpu_offload</code> juga berlaku untuk mereka dan <a href="/docs/transformers/main/id/main_classes/model#transformers.PreTrainedModel.save_pretrained">save_pretrained()</a> mengumpulkannya seperti parameter lainnya.</li> <li>Grup paralel ahli memegang <code>ep_size / tp_size</code> batch, sehingga gradien dari sebuah ahli adalah jumlah dari sebanyak itu batch. Reduksi <code>efsdp</code> membagi dengan <code>fsdp_size</code> alih-alih ukuran grupnya, yang menghasilkan rata-rata per-batch yang sama dengan yang diambil FSDP2 untuk modul padat.</li> <li>Setiap pakar lokal juga memproses satu baris nol pad per lapisan. Rank yang pakarnya tidak menerima token apa pun tetap bergabung dalam reverse all-to-all dan pengurangan gradien pakar.</li></ul> <!--[2--><h3 class="relative group"><a id="how-the-sizes-combine" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#how-the-sizes-combine"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Bagaimana ukuran-ukuran tersebut digabungkan<!----></span></h3><!--]--><!----> <ul><li><code>tp_size * fsdp_size</code> adalah jumlah proses. <code>ep_size</code> tidak menambah apa pun: ia hanya mengelompokkan ulang rank yang sama untuk bobot pakar saja.</li> <li><code>ep_size</code> memotong daftar expert menjadi <code>ep_size</code> blok. Setiap rank menghitung <code>num_experts / ep_size</code> expert, dan <code>ep_size</code> rank berurutan memegang satu set lengkap. Set rank tersebut adalah grup tempat all-to-all dijalankan.</li> <li><code>efsdp_size = fsdp_size * tp_size / ep_size</code> adalah jumlah salinan lengkap dari set expert. Rank pada posisi yang sama di salinan yang berbeda melakukan sharding terhadap expert tersebut untuk memori dan merata-ratakan gradien mereka, seperti yang dilakukan FSDP untuk modul dense.</li> <li>Batch yang dipegang oleh sebuah rank hanya bergantung pada <code>fsdp</code>. Rank-rank yang berurutan membentuk grup TP dan menerima batch yang sama; grup-grup <code>fsdp_size</code> menerima batch yang berbeda.</li></ul> <p>Delapan proses, <code>tp_size=2, fsdp_size=4</code>, delapan ahli:</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> Disalin</div><!----></button><!----></div> <pre class="language-text "><!---->rank 0 1 2 3 4 5 6 7
batch [====B0====] [====B1====] [====B2====] [====B3====] one batch per TP pair
tp 0 1 0 1 0 1 0 1
ep_size=2 E0-3 E4-7 E0-3 E4-7 E0-3 E4-7 E0-3 E4-7 group = a TP pair, efsdp_size=4
ep_size=4 E0E1 E2E3 E4E5 E6E7 E0E1 E2E3 E4E5 E6E7 group = two pairs, efsdp_size=2
ep_size=8 E0 E1 E2 E3 E4 E5 E6 E7 group = all ranks, efsdp_size=1<!----></pre></div><!----> <p>Dua angka dapat disimpulkan dari gambar:</p> <ul><li>Di dalam grup EP, setiap token ada <code>tp_size</code> kali, satu kali per rank dari pasangan yang memegang batch-nya. Hal ini tidak bergantung pada <code>ep_size</code>.</li> <li>Grup EP memegang <code>ep_size / tp_size</code> batch yang berbeda. Ini adalah jumlah yang dijumlahkan oleh gradien seorang expert.</li></ul> <!--[2--><h3 class="relative group"><a id="with-tensor-parallelism" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#with-tensor-parallelism"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Dengan paralelisme tensor<!----></span></h3><!--]--><!----> <p>Setel <code>tp_size > 1</code> untuk memecah modul padat dengan rencana TP, sementara para ahli menggunakan dispatch. Pada delapan proses:</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!---->distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">2</span>,
fsdp_size=<span class="hljs-number">4</span>,
ep_size=<span class="hljs-number">4</span>,
)<!----></pre></div><!----> <p>Setiap pasangan rank TP menerima batch yang sama, karena paralelisme tensor mereplikasi aktivasi di dalam pasangan tersebut. Jika kedua rank mendispatch semua token mereka, rank yang memiliki sebuah expert akan menerima setiap token dua kali, menghitungnya dua kali, dan gradien bobotnya akan menjadi dua kali lipat. Expert utuh berada di satu rank, sehingga duplikat tidak dapat dipecah berdasarkan bobot, dan pemiliknya biasanya adalah rank lain, sehingga tidak dapat diselesaikan berdasarkan kepemilikan seperti yang dilakukan masking. Oleh karena itu, pasangan tersebut membagi baris: setiap rank TP mendispatch <code>1 / tp_size</code> token yang tidak tumpang tindih, hasil kembali ke rank yang mengirimkannya, dan all-reduce di atas pasangan dari separuh yang di-nolkan memulihkan output yang direplikasi yang diharapkan oleh lapisan berikutnya. Pembagian dilakukan berdasarkan <code>tp_size</code>, bukan <code>ep_size</code>, karena hanya rank yang memegang batch yang dapat mengirimkannya. Grup expert membentang di empat rank dan setiap expert di-shard FSDP di <code>efsdp_size = 2</code> rank, sementara grup FSDP trunk membentang di empat rank. Kendala TP biasa dari model, seperti keterbagian kepala perhatian, masih berlaku untuk modul padat. Irisan token mungkin tidak rata atau kosong, termasuk selama decoding token tunggal.</p> <p>Token dispatch belum dapat dikombinasikan dengan pipeline parallelism; gunakan <code>pp_size=1</code> (belum diuji)
Konfigurasi ini masing-masing menggunakan delapan GPU:</p> <table><thead><tr><th align="left">Konfigurasi</th><th align="left">Hasil</th></tr></thead><tbody><tr><td align="left"><code>DistributedConfig(tp_size=4, fsdp_size=2, ep_size=4)</code></td><td align="left">Setiap grup TP berisi empat rank berbagi satu batch. Pelatihan mendistribusikan token, di mana setiap rank mengambil seperempat dari batch; inferensi menggunakan <code>"all_reduce"</code>; pengaturan <code>ep_implementation="all_reduce"</code> menggunakannya di seluruh proses.</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=1, fsdp_size=8, ep_size=4)</code></td><td align="left">Dispatch dengan batch independen di setiap rank, tanpa slicing, dan para ahli yang di-shard FSDP di antara pasangan rank.</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=2, fsdp_size=4, ep_size=4)</code></td><td align="left">Dispatch dengan satu pasangan TP per batch, di mana setiap pasangan memotong batch-nya menjadi dua; dua batch per grup ahli.</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=8, ep_size=8)</code></td><td align="left">Setiap rank berbagi satu batch. Pelatihan mendistribusikan token, di mana setiap rank mengambil seperdelapan dari batch; inferensi menggunakan <code>"all_reduce"</code>; pengaturan <code>ep_implementation="all_reduce"</code> menggunakannya di seluruh proses.</td></tr></tbody></table> <blockquote class="warning"><p><a href="/docs/transformers/main/id/main_classes/trainer#transformers.Trainer">Trainer</a> belum memperhitungkan dispatch token: penghitungan batch dan token mengasumsikan tata letak all-reduce, di mana rank dari grup TP berbagi satu batch dan terdapat <code>fsdp_size</code> shard data-parallel. Dukungan Trainer untuk dispatch akan hadir pada pembaruan berikutnya.</p></blockquote> <!--[1--><h2 class="relative group"><a id="combining-with-fsdp2" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#combining-with-fsdp2"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Menggabungkan dengan FSDP2<!----></span></h2><!--]--><!----> <p>Tensor dan expert parallelism membagi (shard) bobot di atas mesh mereka sendiri, <code>tp</code> dan <code>ep</code>, tetapi state optimizer dan modul tanpa aturan masih direplikasi di setiap rank dari grup tersebut, yang membatasi seberapa besar model yang dapat dilatih. Tambahkan <a href="./fsdp">FSDP2</a> pada dimensi mesh lain dengan <code>fsdp_size</code>. Dengan masking dan all-reduce, pertahankan <code>ep_size=tp_size</code> dan lewati <code>ep_implementation="all_reduce"</code>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
<span class="hljs-keyword">from</span> transformers.distributed <span class="hljs-keyword">import</span> DistributedConfig
distributed_config = DistributedConfig(
tp_size=<span class="hljs-number">4</span>,
ep_size=<span class="hljs-number">4</span>, <span class="hljs-comment"># expert parallel size, must match tp_size with masking and all-reduce</span>
fsdp_size=<span class="hljs-number">2</span>, <span class="hljs-comment"># data parallel shards</span>
ep_implementation=<span class="hljs-string">&quot;all_reduce&quot;</span>, <span class="hljs-comment"># &quot;auto&quot; would dispatch tokens in training, which the Trainer does not support yet</span>
)
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen3-30B-A3B&quot;</span>, distributed_config=distributed_config)<!----></pre></div><!----> <p>Muat model seperti biasa, lalu latih dengan <a href="/docs/transformers/main/id/main_classes/trainer#transformers.Trainer">Trainer</a>. Proses ini mengambil norma gradien di kedua mesh dan memberikan grup parameter optimizer yang terpisah untuk masing-masing mesh. <a href="/docs/transformers/main/id/main_classes/trainer#transformers.Trainer.save_model">save_model()</a> mengumpulkan bobot yang di-shard menjadi checkpoint reguler. Hal ini memerlukan <code>accelerate>=1.12</code> agar <code>Trainer</code> dapat mencerminkan <code>tp_size</code> dan <code>fsdp_size</code> ke dalam <code>~Accelerate.ParallelismConfig</code>.</p> <p>Tabel di bawah ini membandingkan pelatihan EP-only dengan 2D EP+FSDP2 pada 8xH100 GPU. Beban kerja adalah fine-tuning penuh Qwen3-30B-A3B dalam bf16 pada panjang sekuens 2048. Semakin banyak shard FSDP, semakin rendah memori puncak, dan tokens/s turun sedikit karena FSDP2 melakukan all-gathers dan reduce-scatters para ahli di seluruh <code>fsdp</code>.</p> <table><thead><tr><th>konfigurasi</th><th>tokens/s/GPU</th><th>memori puncak/GPU</th></tr></thead><tbody><tr><td><code>tp_size=8</code></td><td>3485</td><td>38,6 GB</td></tr><tr><td><code>tp_size=4, fsdp_size=2</code></td><td>2900</td><td>34,2 GB</td></tr><tr><td><code>tp_size=2, fsdp_size=4</code></td><td>2830</td><td>32,3 GB</td></tr></tbody></table> <blockquote class="warning"><p>Melanjutkan dari checkpoint belum didukung untuk model yang di-shard saat dimuat, sehingga <a href="/docs/transformers/main/id/main_classes/trainer#transformers.Trainer">Trainer</a> hanya menerima <code>save_only_model=True</code> atau <code>save_strategy="no"</code> untuk model tersebut.</p></blockquote> <!--[1--><h2 class="relative group"><a id="transformers.DistributedConfig" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Referensi API<!----></span></h2><!--]--><!----> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"><div><span class="group flex space-x-1.5 items-center text-gray-800 bg-gradient-to-r rounded-tr-lg -mt-4 -ml-4 pt-3 px-2.5" id="transformers.DistributedConfig"><!----><h3 class="!m-0"><span class="flex-1 break-all md:text-lg bg-gradient-to-r px-2.5 py-1.5 rounded-xl from-indigo-50/70 to-white dark:from-gray-900 dark:to-gray-950 dark:text-indigo-300 text-indigo-700"><svg class="mr-1.5 text-indigo-500 inline-block -mt-0.5" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width=".8em" height=".8em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path class="uim-quaternary" d="M20.23 7.24L12 12L3.77 7.24a1.98 1.98 0 0 1 .7-.71L11 2.76c.62-.35 1.38-.35 2 0l6.53 3.77c.29.173.531.418.7.71z" opacity=".25" fill="currentColor"></path><path class="uim-tertiary" d="M12 12v9.5a2.09 2.09 0 0 1-.91-.21L4.5 17.48a2.003 2.003 0 0 1-1-1.73v-7.5a2.06 2.06 0 0 1 .27-1.01L12 12z" opacity=".5" fill="currentColor"></path><path class="uim-primary" d="M20.5 8.25v7.5a2.003 2.003 0 0 1-1 1.73l-6.62 3.82c-.275.13-.576.198-.88.2V12l8.23-4.76c.175.308.268.656.27 1.01z" fill="currentColor"></path></svg><span class="font-light">kelas</span> <span class="font-medium">transformers.</span><span class="font-semibold">DistributedConfig</span></span></h3><!----> <a id="transformers.DistributedConfig" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.DistributedConfig"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></a> <!--[0--><a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/main/src/transformers/distributed/configuration_utils.py#L26" target="_blank"><span>&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline">source</span> <span>></span></a><!--]--></span> <!--[0--><p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span>(</span> <!--[--><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">tp_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">tp_plan<span class="opacity-60">: typing.Union[dict[str, str], typing.Literal['auto'], NoneType] = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">enable_sequence_parallel<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">enable_expert_parallel<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">dp_replicate_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_plan<span class="opacity-60">: dict[str, str] | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_cpu_offload<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">fsdp_mixed_precision<span class="opacity-60">: bool = False</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">pp_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">ep_size<span class="opacity-60">: int | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">ep_plan<span class="opacity-60">: dict[str, str] | None = None</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">ep_implementation<span class="opacity-60">: str = 'auto'</span></span></span><!--]--> <span>)</span> <!--[-1--><!--]--></p><!--]--> <div class="!mb-10 relative docstring-details "><!--[-1--><!--]--> <!--[0--><p class="flex items-center font-semibold !mt-2 !mb-2 text-gray-800">Parameter <span class="flex-auto border-t-2 border-gray-100 dark:border-gray-700 ml-3"></span></p> <ul class="px-2"><!--[--><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.tp_size" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.tp_size"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>tp_size</strong> (<code>int</code>, <em>opsional</em>) —
Jumlah perangkat untuk tensor parallelism. Jika <code>None</code> dan <code>tp_plan</code> diset, defaultnya adalah
<code>WORLD_SIZE // (other_parallel_size)</code>. Jika <code>None</code> dan tidak ada <code>tp_plan</code> yang diset, defaultnya adalah 1.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.tp_plan" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.tp_plan"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>tp_plan</strong> (<code>dict[str, str]</code> atau <code>&quot;auto&quot;</code>, <em>opsional</em>) —
Rencana sharding paralel tensor. Berikan <code>&quot;auto&quot;</code>, atau biarkan sebagai <code>None</code> ketika <code>tp_size</code> disetel, untuk menggunakan
<code>base_model_tp_plan</code> yang telah didefinisikan sebelumnya pada model. Berikan kamus untuk menimpa aturan individual dari rencana tersebut;
aturan yang tidak ditentukan akan dipertahankan. Kamus kosong tidak menerapkan aturan paralel tensor apa pun.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.enable_sequence_parallel" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.enable_sequence_parallel"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>enable_sequence_parallel</strong> (<code>bool</code>, <em>opsional</em>, default <code>False</code>) —
Ditentukan untuk sequence parallelism. Belum diintegrasikan.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.enable_expert_parallel" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.enable_expert_parallel"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>enable_expert_parallel</strong> (<code>bool</code>, <em>opsional</em>, default <code>False</code>) —
Tidak lagi didukung, dihapus di v5.21. Tanpa <code>ep_size</code>, sama dengan <code>ep_size=tp_size, tp_plan={}</code>:
rencana expert-parallel saja di seluruh <code>tp_size</code> rank. Nilai <code>ep_size</code> yang eksplisit memiliki prioritas. Flag ini
tidak mengubah <code>tp_size</code> atau <code>fsdp_size</code>.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_size" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.fsdp_size"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>fsdp_size</strong> (<code>int</code>, <em>opsional</em>) —
Jumlah perangkat untuk FSDP (paralelisme data). Jika <code>None</code> dan <code>tp_size</code> diatur, defaultnya adalah 1.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.dp_replicate_size" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.dp_replicate_size"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>dp_replicate_size</strong> (<code>int</code>, <em>opsional</em>) —
Jumlah replika FSDP (HSDP). Parameter di-shard di antara <code>fsdp_size</code> perangkat dan direplikasi
<code>dp_replicate_size</code> kali: gradien di-reduce-scatter di dalam sebuah replika, kemudian di-all-reduce di antara
replika. Nilai default adalah 1. Kami menyarankan ini untuk pelatihan multi-node: FSDP biasa melakukan all-gather parameter dan
reduce-scatter gradien di antara semua perangkat pada setiap langkah, sehingga lalu lintas ini melewati
link inter-node yang lebih lambat. Atur <code>dp_replicate_size</code> ke jumlah node dan <code>fsdp_size * tp_size</code> ke jumlah
perangkat per node: lalu lintas sharding kemudian tetap berada di link intra-node yang cepat, dan hanya all-reduce gradien
yang menyeberangi antar node. Biayanya adalah lebih banyak memori per perangkat, karena parameter di-shard di dalam sebuah node.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_plan" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.fsdp_plan"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>fsdp_plan</strong> (<code>dict[str, str]</code>, <em>opsional</em>) —
Rencana sharding FSDP2. Biarkan sebagai <code>None</code> untuk menggunakan <code>base_model_fsdp_plan</code> yang telah ditentukan sebelumnya pada model dan
<code>_fsdp_plan</code> pada level kelas. Teruskan sebuah kamus untuk menimpa aturan individual; aturan yang tidak ditentukan akan dipertahankan.
Diterapkan hanya ketika <code>dp_replicate_size * fsdp_size &gt; 1</code>.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_cpu_offload" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.fsdp_cpu_offload"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>fsdp_cpu_offload</strong> (<code>bool</code>, <em>opsional</em>, default <code>False</code>) —
Apakah akan mengaktifkan CPU offloading untuk FSDP2.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.fsdp_mixed_precision" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.fsdp_mixed_precision"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>fsdp_mixed_precision</strong> (<code>bool</code>, <em>opsional</em>, default <code>False</code>) —
Apakah akan mengaktifkan presisi campuran untuk FSDP2.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.pp_size" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.pp_size"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>pp_size</strong> (<code>int</code>, <em>opsional</em>) —
Jumlah perangkat untuk pipeline parallelism. Jika <code>None</code> dan mode paralel lain diatur, defaultnya adalah 1.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.ep_size" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.ep_size"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>ep_size</strong> (<code>int</code>, <em>opsional</em>) —
Jumlah perangkat yang memiliki shard expert yang berbeda. Defaultnya adalah 1. Tetapkan secara eksplisit untuk mengaktifkan EP. Harus menjadi kelipatan dari <code>tp_size</code> dan membagi <code>fsdp_size * tp_size</code>. <code>ep_size &gt; tp_size</code> memerlukan aturan <code>&quot;ep_dispatch_experts&quot;</code> dengan <code>ep_implementation</code> yang menukar token, seperti <code>&quot;all_to_all&quot;</code>.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.ep_plan" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.ep_plan"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>ep_plan</strong> (<code>dict[str, str]</code>, <em>opsional</em>) —
Rencana sharding paralelisme ahli. Biarkan sebagai <code>None</code> untuk menggunakan <code>base_model_ep_plan</code> yang telah didefinisikan sebelumnya pada model. Teruskan
kamus untuk menimpa aturan individual dari rencana tersebut; aturan yang tidak ditentukan akan dipertahankan. Diterapkan hanya ketika
<code>ep_size &gt; 1</code>, dan aturannya memiliki prioritas lebih tinggi daripada aturan <code>tp_plan</code> untuk modul yang sama. Aturan
<code>&quot;ep_dispatch_experts&quot;</code> memungkinkan <code>ep_implementation</code> memilih bagaimana token mencapai para ahli.<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.DistributedConfig.ep_implementation" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.DistributedConfig.ep_implementation"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!----><strong>ep_implementation</strong> (<code>str</code>, <em>opsional</em>, default ke <code>&quot;auto&quot;</code>) —
Bagaimana aturan <code>&quot;ep_dispatch_experts&quot;</code> mengirimkan token ke para expert. Ini menamai sebuah <code>EPImplementation</code>
yang terdaftar di <code>EPImplementationInterface</code>:
<ul>
<li><code>&quot;all_to_all&quot;</code> menukar token yang di-rutekan dengan kolektif all-to-all.</li>
<li><code>&quot;all_reduce&quot;</code> menjalankan para ahli dari setiap rank pada batch yang dibagikan oleh grup EP, lalu menjumlahkan outputnya. Ini
memerlukan <code>ep_size == tp_size</code>.</li>
<li><code>&quot;deepgemm_megamoe&quot;</code> menyerahkan batch tersebut ke DeepGEMM Mega MoE, yang kernel-nya menukar token secara mandiri.
Memuat dengan <code>experts_implementation=&quot;deepgemm_megamoe&quot;</code> memilihnya.</li>
<li><code>&quot;auto&quot;</code> menjalankan <code>&quot;all_reduce&quot;</code> saat inferensi jika grup EP berbagi satu batch, dan <code>&quot;all_to_all&quot;</code>
jika tidak.</li>
</ul><!----></span></span></li><!--]--></ul><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--></div></div><!----> <p>Konfigurasi untuk inferensi dan pelatihan terdistribusi native dengan paralelisme tensor, pipeline, atau FSDP2.</p></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/expert_parallelism.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_p1l6wl = {
base: "/docs/transformers/main/en",
assets: "/docs/transformers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/transformers/main/en/_app/immutable/entry/start.DS27pc2p.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 5],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
79.2 kB
·
Xet hash:
c9b49ee5f71ed7dc0445e9b9593248e95dd5fe83a04e4b94df97b9d77c5ce519

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