Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Song song chuyên gia","local":"expert-parallelism","sections":[{"title":"DistributedConfig","local":"distributedconfig","sections":[{"title":"Chọn một triển khai","local":"choosing-an-implementation","sections":[],"depth":3},{"title":"Thêm một cài đặt","local":"adding-an-implementation","sections":[],"depth":3},{"title":"Ghi đè các kế hoạch","local":"overriding-the-plans","sections":[],"depth":3}],"depth":2},{"title":"Phân phối token","local":"token-dispatch","sections":[{"title":"Cách các kích thước kết hợp","local":"how-the-sizes-combine","sections":[],"depth":3},{"title":"Với song song tensor","local":"with-tensor-parallelism","sections":[],"depth":3}],"depth":2},{"title":"Kết hợp với FSDP2","local":"combining-with-fsdp2","sections":[],"depth":2},{"title":"Tham khảo API","local":"transformers.DistributedConfig","sections":[],"depth":2}],"depth":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="{"title":"Expert parallelism","local":"expert-parallelism","sections":[{"title":"DistributedConfig","local":"distributedconfig","sections":[{"title":"Choosing an implementation","local":"choosing-an-implementation","sections":[],"depth":3},{"title":"Adding an implementation","local":"adding-an-implementation","sections":[],"depth":3},{"title":"Overriding the plans","local":"overriding-the-plans","sections":[],"depth":3}],"depth":2},{"title":"Token dispatch","local":"token-dispatch","sections":[{"title":"How the sizes combine","local":"how-the-sizes-combine","sections":[],"depth":3},{"title":"With tensor parallelism","local":"with-tensor-parallelism","sections":[],"depth":3}],"depth":2},{"title":"Combining with FSDP2","local":"combining-with-fsdp2","sections":[],"depth":2},{"title":"API reference","local":"transformers.DistributedConfig","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="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><!---->Song song chuyên gia<!----></span></h1><!--]--><!----> <p><a href="https://huggingface.co/spaces/nanotron/ultrascale-playbook?section=expert_parallelism" rel="nofollow">Expert parallelism</a> là một chiến lược song song hóa cho các <a href="https://huggingface.co/blog/moe" rel="nofollow">mô hình mixture-of-experts (MoE)</a>. Lớp feedforward của mỗi expert nằm trên một bộ tăng tốc phần cứng khác nhau. Một router sẽ điều phối các token đến các expert phù hợp và thu thập kết quả. Cách tiếp cận này cho phép mở rộng mô hình lên số lượng tham số lớn hơn nhiều mà không làm tăng chi phí tính toán, vì mỗi token chỉ kích hoạt một vài 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>Bật song song chuyên gia bằng lớp <a href="/docs/transformers/main/vi/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a> và đối số <code>ep_size</code>. Các mô hình MoE chia nhỏ chuyên gia của chúng bằng <code>"ep_dispatch_experts"</code>. Các cài đặt của nó quyết định cách các token đến các chuyên gia. <a href="#token-dispatch">Token dispatch</a> cho phép <code>ep_size</code> khác với <code>tp_size</code>. Một số cài đặt EP yêu cầu <code>ep_size=tp_size</code>, ví dụ <code>"all_reduce"</code> và <code>"deepgemm_megamoe"</code>, để mọi rank trong một nhóm chuyên gia giữ cùng các token.</p> <p>Llama 4 không có kế hoạch EP: các chuyên gia của nó nhận mọi token đã được nhân theo điểm router, nên không có gì để điều phối. Hãy chia nhỏ chúng bằng song song tensor (<code>tp_size</code>) thay vào đó.</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> Đã sao chép</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">"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">"openai/gpt-oss-120b"</span>, | |
| distributed_config=distributed_config, | |
| )<!----></pre></div><!----> <p>Mỗi mô hình MoE định nghĩa hai kế hoạch trong cấu hình của nó: <code>base_model_tp_plan</code> cho các module dense và <code>base_model_ep_plan</code> cho các chuyên gia. Chúng được hiển thị trên mô hình đã tải dưới dạng <code>model.tp_plan</code> và <code>model.ep_plan</code>. Với <code>tp_size > 1</code> và <code>ep_size > 1</code>, cả hai đều được áp dụng: kế hoạch <a href="./perf_infer_gpu_multi">tensor parallel</a> chia nhỏ attention và các MLP dense, còn kế hoạch expert parallel chia nhỏ các chuyên gia. Các quy tắc EP có ưu tiên cao hơn các quy tắc TP đối với cùng một module, do đó trọng số chuyên gia chỉ được chia nhỏ một lần, bởi kế hoạch EP. Trong kế hoạch EP, kiểu <code>GroupedGemmParallel</code> chia nhỏ trọng số chuyên gia dọc theo chiều chuyên gia để mỗi rank chỉ tải các chuyên gia cục bộ của nó, và quy tắc <code>"ep_dispatch_experts"</code> trên module chuyên gia xác định cách các token được chuyển đến chúng.</p> <p><code>tp_plan</code> chỉ được áp dụng khi <code>tp_size > 1</code>, và <code>ep_plan</code> chỉ khi <code>ep_size > 1</code>. Khi TP được bật và EP bị tắt, toàn bộ kế hoạch TP sẽ được áp dụng, bao gồm cả các quy tắc chuyên gia.</p> <p><code>"ep_dispatch_experts"</code> chia các chuyên gia nguyên vẹn theo từng rank (<code>ep_size</code> là bội số của <code>tp_size</code> và chia hết <code>fsdp_size * tp_size</code>) và truy cập chúng thông qua cài đặt do <code>ep_implementation</code> chỉ định, xem <a href="#choosing-an-implementation">Chọn một cài đặt</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><!---->Chọn một triển khai<!----></span></h3><!--]--><!----> <p><code>"ep_dispatch_experts"</code> chia nhỏ các chuyên gia một lần và chọn cách các token tiếp cận chúng từ các cài đặt được đăng ký trong <code>EPImplementationInterface</code>, được chọn bằng <code>ep_implementation</code> trong <a href="/docs/transformers/main/vi/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a>:</p> <table><thead><tr><th align="left"><code>ep_implementation</code></th><th align="left">lợi thế</th><th align="left">nhược điểm</th></tr></thead><tbody><tr><td align="left"><code>"all_to_all"</code>, xem <a href="#token-dispatch">phân phối token</a></td><td align="left">Hoạt động với mọi bố cục, bao gồm cả <code>ep_size > tp_size</code>. Chỉ các token được định tuyến mới di chuyển.</td><td align="left">Hai tập thể all-to-all và một đồng bộ hóa host cho mỗi lớp, do đó nó không được ghi nhận trong một đồ thị CUDA.</td></tr><tr><td align="left"><code>"all_reduce"</code></td><td align="left">Không có token nào di chuyển và không có đồng bộ hóa trên host, do đó quá trình giải mã được ghi lại trong một đồ thị CUDA.</td><td align="left">Yêu cầu <code>ep_size=tp_size</code>. Mỗi rank chạy các chuyên gia của nó trên toàn bộ batch, do đó chi phí của nó tăng theo kích thước batch.</td></tr><tr><td align="left"><code>"deepgemm_megamoe"</code></td><td align="left">DeepGEMM Mega MoE hợp nhất các bước dispatch, experts và combine trong một kernel duy nhất. Mỗi lớp của một nhóm EP chia sẻ các bộ đệm bộ nhớ đối xứng của nó.</td><td align="left">Yêu cầu <code>ep_size=tp_size</code>, và kích hoạt FP8 với chuyên gia FP4 trên SM100+. Chỉ dành cho suy luận.</td></tr><tr><td align="left"><code>"auto"</code>, mặc định</td><td align="left">Chạy <code>"all_reduce"</code> khi suy luận nếu các rank trong một nhóm chuyên gia chia sẻ một batch, và chạy <code>"all_to_all"</code> trong các trường hợp khác.</td><td align="left">Không phải lúc nào cũng là lựa chọn nhanh nhất: một khối lượng công việc có thể được hưởng lợi từ một triển khai tùy chỉnh, xem <a href="#adding-an-implementation">Thêm một triển khai</a>.</td></tr></tbody></table> <blockquote class="note"><p><code>ep_implementation</code> chỉ chọn trong số các kích thước song song được đặt trong <a href="/docs/transformers/main/vi/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a>. Khi các rank của một nhóm chuyên gia giữ các batch khác nhau, ví dụ <code>tp_size=1</code> với <code>fsdp_size=ep_size</code>, <code>"all_to_all"</code> là triển khai duy nhất được áp dụng.</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><!---->Thêm một cài đặt<!----></span></h3><!--]--><!----> <p>Một implementation kế thừa <code>EPImplementation</code> và đăng ký dưới tên <code>ep_implementation</code> được chọn. Nó được xây dựng một lần cho mỗi module experts, do đó có thể giữ trạng thái như các bộ đệm truyền thông. Khi được gọi với batch mà rank này đang giữ, cùng với các expert id toàn cục, nó trả về đầu ra expert đã được trọng số hóa cho từng token của nó. Nó chạy các experts của rank này bằng <code>experts_forward</code>, trên các hàng, expert id cục bộ và trọng số mà nó đã mang đến cho chúng.</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> Đã sao chép</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'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">"my_ep"</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">"my_ep"</span>)<!----></pre></div><!----> <blockquote class="note"><p>Gradient phải đến được mọi rank đang giữ một token, bao gồm cả trọng số định tuyến, vì vậy hãy bọc một phép toán tập thể mà autograd không phân biệt trong một <code>torch.autograd.Function</code>.</p></blockquote> <blockquote class="tip"><p><code>enable_expert_parallel=True</code> mà không có <code>ep_size</code> đã bị ngừng hỗ trợ và sẽ phát ra <code>FutureWarning</code>. Nó chỉ chia nhỏ các chuyên gia, giống như <code>ep_size=tp_size, tp_plan={}</code>.</p></blockquote> <p>Chạy script suy luận của bạn bằng <a href="https://pytorch.org/docs/stable/elastic/run.html" rel="nofollow">torchrun</a>. Số lượng tiến trình phải bằng <code>tp_size * fsdp_size * pp_size</code>, và <code>ep_size</code> phải chia hết cho số lượng chuyên gia.</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> Đã sao chép</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><!---->Ghi đè các kế hoạch<!----></span></h3><!--]--><!----> <p>Truyền <code>tp_plan={...}</code> hoặc <code>ep_plan={...}</code> vào <a href="/docs/transformers/main/vi/expert_parallelism#transformers.DistributedConfig">DistributedConfig</a> để ghi đè các quy tắc riêng lẻ của các kế hoạch được định nghĩa trước. Các quy tắc không được chỉ định sẽ được giữ nguyên, và các kế hoạch đã hợp nhất sẽ được lưu trên model. Mỗi key phải khớp với một module, một parameter, hoặc một mục có sẵn trong kế hoạch; nếu không, quá trình tải sẽ gây ra <code>ValueError</code> trước khi bất kỳ phần nào được sharding. Hãy sử dụng đường dẫn đầy đủ như nhìn từ model đã được tải, ví dụ <code>model.layers.*</code> cho một causal LM và <code>layers.*</code> cho base model của nó.</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> Đã sao chép</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">"model.layers.*.self_attn.q_proj"</span>: <span class="hljs-string">"colwise_rep"</span>}, | |
| ep_plan={<span class="hljs-string">"model.layers.*.mlp.experts.down_proj"</span>: <span class="hljs-string">"grouped_gemm"</span>}, | |
| )<!----></pre></div><!----> <p>Việc cung cấp một kế hoạch không suy ra kích thước song song: hãy đặt <code>tp_size</code> và <code>ep_size</code> một cách rõ ràng.</p> <p>Với <code>ep_size=tp_size</code>, cài đặt mặc định <code>"auto"</code> chạy <code>"all_reduce"</code> ở giai đoạn suy luận và phân phối token trong huấn luyện. Để che (mask) và all-reduce cả trong huấn luyện, hãy đặt <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> Đã sao chép</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">"all_reduce"</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><!---->Phân phối token<!----></span></h2><!--]--><!----> <p>Token dispatch là phần triển khai <code>"all_to_all"</code>: mỗi rank huấn luyện trên phần riêng của batch. Tại mỗi lớp MoE, một rank định tuyến các token của nó, gửi mỗi cặp (token, expert) đến rank sở hữu expert đó bằng all-to-all, chạy các expert cục bộ trên những gì nó nhận được, nhận kết quả trở lại bằng một all-to-all thứ hai và kết hợp chúng với trọng số định tuyến. Chỉ các activation được định tuyến và đầu ra của expert di chuyển, và không rank nào tính toán expert cho các token mà nó không sở hữu.</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> Đã sao chép</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">"Qwen/Qwen3-30B-A3B"</span>, distributed_config=distributed_config)<!----></pre></div><!----> <p>Với <code>tp_size=1</code>, <code>ep_size</code> phải chia hết cho <code>fsdp_size</code> và số lượng chuyên gia, và attention không bị ràng buộc bởi <code>num_key_value_heads</code>. Đối với phần còn lại của mô hình:</p> <ul><li>Các tham số nằm ngoài các chuyên gia được chia nhỏ bằng <a href="./fsdp">FSDP2</a> trên <code>fsdp</code>, điều này làm giảm gradient của chúng.</li> <li>Các chuyên gia được chia nhỏ (sharded) trên <code>ep</code> và, khi <code>efsdp_size = fsdp_size * tp_size // ep_size</code> lớn hơn một, chúng còn được chia nhỏ theo FSDP trên <code>efsdp</code>. Chúng luôn được bọc (wrapped) bởi FSDP, do đó <code>fsdp_mixed_precision</code> và <code>fsdp_cpu_offload</code> cũng áp dụng cho chúng và <a href="/docs/transformers/main/vi/main_classes/model#transformers.PreTrainedModel.save_pretrained">save_pretrained()</a> sẽ thu thập (gather) chúng giống như bất kỳ tham số nào khác.</li> <li>Một nhóm song song chuyên gia giữ <code>ep_size / tp_size</code> lô, do đó gradient của một chuyên gia là tổng trên số lô đó. Việc giảm <code>efsdp</code> chia cho <code>fsdp_size</code> thay vì kích thước nhóm của nó, cho ra cùng một giá trị trung bình trên mỗi lô mà FSDP2 áp dụng cho các module đặc.</li> <li>Mỗi chuyên gia cục bộ cũng xử lý một hàng đệm zero cho mỗi lớp. Một rank mà các chuyên gia của nó không nhận được token nào vẫn tham gia vào quá trình reverse all-to-all và giảm gradient chuyên gia.</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><!---->Cách các kích thước kết hợp<!----></span></h3><!--]--><!----> <ul><li><code>tp_size * fsdp_size</code> là số lượng tiến trình. <code>ep_size</code> không thêm gì: nó chỉ nhóm lại các rank giống nhau cho trọng số chuyên gia.</li> <li><code>ep_size</code> chia danh sách chuyên gia thành <code>ep_size</code> khối. Mỗi rank tính toán <code>num_experts / ep_size</code> chuyên gia, và <code>ep_size</code> rank liên tiếp giữ một bộ đầy đủ. Tập hợp các rank đó là nhóm mà all-to-all chạy trong đó.</li> <li><code>efsdp_size = fsdp_size * tp_size / ep_size</code> là số lượng bản sao đầy đủ của tập chuyên gia tồn tại. Các rank ở cùng vị trí trong các bản sao khác nhau sẽ chia nhỏ (shard) các chuyên gia đó để tiết kiệm bộ nhớ và trung bình hóa gradient của chúng, giống như cách FSDP thực hiện cho các module dense.</li> <li>Batch mà một rank giữ phụ thuộc duy nhất vào <code>fsdp</code>. Các rank liên tiếp tạo thành một nhóm TP và nhận cùng một batch; <code>fsdp_size</code> nhóm nhận các batch khác nhau.</li></ul> <p>Tám tiến trình, <code>tp_size=2, fsdp_size=4</code>, tám chuyên gia:</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> Đã sao chép</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>Hai con số rút ra từ hình ảnh:</p> <ul><li>Bên trong một nhóm EP, mỗi token tồn tại <code>tp_size</code> lần, một lần cho mỗi rank của cặp giữ batch của nó. Điều này không phụ thuộc vào <code>ep_size</code>.</li> <li>Một nhóm EP giữ <code>ep_size / tp_size</code> các lô dữ liệu khác nhau. Đây là số lượng mà gradient của một chuyên gia được cộng dồn.</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><!---->Với song song tensor<!----></span></h3><!--]--><!----> <p>Đặt <code>tp_size > 1</code> để chia nhỏ các module dense theo kế hoạch TP trong khi các expert sử dụng dispatch. Trên tám tiến trình:</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> Đã sao chép</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>Mỗi cặp rank TP nhận cùng một batch, vì tensor parallelism nhân bản các activation bên trong cặp đó. Nếu cả hai rank đều dispatch toàn bộ token của mình, rank sở hữu một expert sẽ nhận mỗi token hai lần, tính toán hai lần, và gradient trọng số của nó sẽ bị nhân đôi. Các expert nằm trọn trên một rank, nên bản sao thừa không thể được chia theo trọng số, và rank sở hữu thường là một rank khác, nên cũng không thể giải quyết bằng quyền sở hữu như cách masking làm. Do đó, cặp này chia các hàng: mỗi rank TP dispatch một phần rời rạc <code>1 / tp_size</code> của các token, kết quả được trả về rank đã gửi, và một all-reduce trên cặp của các nửa được zero-pad khôi phục đầu ra được nhân bản mà lớp tiếp theo kỳ vọng. Việc chia được thực hiện theo <code>tp_size</code>, không phải <code>ep_size</code>, vì chỉ các rank giữ một batch mới có thể gửi nó. Các nhóm expert trải dài trên bốn rank và mỗi expert được FSDP-sharded trên <code>efsdp_size = 2</code> rank, trong khi nhóm FSDP của trunk trải dài trên bốn rank. Các ràng buộc TP thông thường của mô hình, chẳng hạn như tính chia hết của attention-head, vẫn áp dụng cho các module dense. Các lát token có thể không đều hoặc rỗng, kể cả trong quá trình giải mã đơn token.</p> <p>Token dispatch hiện chưa thể kết hợp với pipeline parallelism; hãy sử dụng <code>pp_size=1</code> (chưa được kiểm tra) | |
| Các cấu hình này đều sử dụng tám GPU:</p> <table><thead><tr><th align="left">Cấu hình</th><th align="left">Kết quả</th></tr></thead><tbody><tr><td align="left"><code>DistributedConfig(tp_size=4, fsdp_size=2, ep_size=4)</code></td><td align="left">Mỗi nhóm TP gồm bốn phần tử chia sẻ một batch. Trong quá trình huấn luyện, các token được phân phối, mỗi rank nhận một phần tư của batch; suy luận sử dụng <code>"all_reduce"</code>; việc đặt <code>ep_implementation="all_reduce"</code> sẽ sử dụng nó xuyên suốt.</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=1, fsdp_size=8, ep_size=4)</code></td><td align="left">Dispatch với một batch độc lập trên mỗi rank, không có slicing, và các expert được chia nhỏ theo FSDP trên các cặp rank.</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=2, fsdp_size=4, ep_size=4)</code></td><td align="left">Dispatch với một cặp TP cho mỗi batch, mỗi cặp sẽ chia batch của nó thành hai; hai batch cho mỗi nhóm chuyên gia.</td></tr><tr><td align="left"><code>DistributedConfig(tp_size=8, ep_size=8)</code></td><td align="left">Mọi rank chia sẻ một batch duy nhất. Trong quá trình huấn luyện, các token được phân phối, mỗi rank nhận một phần tám của batch; suy luận sử dụng <code>"all_reduce"</code>; việc đặt <code>ep_implementation="all_reduce"</code> sẽ sử dụng nó xuyên suốt.</td></tr></tbody></table> <blockquote class="warning"><p><a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer">Trainer</a> hiện chưa hỗ trợ việc phân phối token: việc đếm batch và token giả định bố cục all-reduce, trong đó các rank của một nhóm TP chia sẻ một batch và tồn tại <code>fsdp_size</code> shard song song dữ liệu. Hỗ trợ dispatch cho Trainer sẽ được bổ sung trong một bản cập nhật tiếp theo.</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><!---->Kết hợp với FSDP2<!----></span></h2><!--]--><!----> <p>Tensor và expert parallelism chia nhỏ (shard) trọng số trên các lưới riêng của chúng, <code>tp</code> và <code>ep</code>, nhưng trạng thái optimizer và các module không có quy tắc vẫn được nhân bản (replicate) trên mọi rank của nhóm, điều này giới hạn kích thước mô hình mà bạn có thể huấn luyện. Thêm <a href="./fsdp">FSDP2</a> trên một chiều lưới khác với <code>fsdp_size</code>. Với masking và all-reduce, giữ <code>ep_size=tp_size</code> và truyền <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> Đã sao chép</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">"all_reduce"</span>, <span class="hljs-comment"># "auto" would dispatch tokens in training, which the Trainer does not support yet</span> | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">"Qwen/Qwen3-30B-A3B"</span>, distributed_config=distributed_config)<!----></pre></div><!----> <p>Tải model như bình thường, sau đó huấn luyện với <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer">Trainer</a>. Nó lấy chuẩn gradient trên cả hai mesh và cấp cho mỗi mesh một nhóm tham số optimizer riêng. <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer.save_model">save_model()</a> thu thập các trọng số đã chia mảnh thành một checkpoint thông thường. Điều này yêu cầu <code>accelerate>=1.12</code> để <code>Trainer</code> có thể sao chép <code>tp_size</code> và <code>fsdp_size</code> vào <code>~Accelerate.ParallelismConfig</code>.</p> <p>Bảng dưới đây so sánh huấn luyện chỉ dùng EP với 2D EP+FSDP2 trên 8 GPU H100. Khối lượng công việc là fine-tuning đầy đủ Qwen3-30B-A3B ở bf16 với độ dài chuỗi 2048. Nhiều shard FSDP hơn sẽ cắt giảm bộ nhớ đỉnh, và tokens/s giảm một chút vì FSDP2 thực hiện all-gather và reduce-scatter các expert qua <code>fsdp</code>.</p> <table><thead><tr><th>cấu hình</th><th>tokens/s/GPU</th><th>bộ nhớ đỉnh/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>Việc tiếp tục từ một checkpoint hiện chưa được hỗ trợ cho các mô hình được chia mảnh (sharded) tại thời điểm tải, do đó <a href="/docs/transformers/main/vi/main_classes/trainer#transformers.Trainer">Trainer</a> chỉ chấp nhận <code>save_only_model=True</code> hoặc <code>save_strategy="no"</code> cho chúng.</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><!---->Tham khảo 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">lớp</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><</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">Tham số <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>tùy chọn</em>) — | |
| Số lượng thiết bị cho tensor parallelism. Nếu <code>None</code> và <code>tp_plan</code> được đặt, mặc định là | |
| <code>WORLD_SIZE // (other_parallel_size)</code>. Nếu <code>None</code> và không có <code>tp_plan</code> nào được đặt, mặc định là 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> hoặc <code>"auto"</code>, <em>tùy chọn</em>) — | |
| Kế hoạch chia nhỏ song song tensor. Truyền <code>"auto"</code>, hoặc để <code>None</code> khi <code>tp_size</code> được đặt, để sử dụng | |
| <code>base_model_tp_plan</code> được định nghĩa trước của mô hình. Truyền một từ điển để ghi đè các quy tắc riêng lẻ của kế hoạch đó; | |
| các quy tắc không được chỉ định sẽ được giữ nguyên. Một từ điển rỗng sẽ không áp dụng quy tắc song song tensor nào.<!----></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>tùy chọn</em>, mặc định là <code>False</code>) — | |
| Dành cho song song hóa chuỗi. Chưa được kết nối.<!----></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>tùy chọn</em>, mặc định là <code>False</code>) — | |
| Đã bị loại bỏ, xóa trong v5.21. Nếu không có <code>ep_size</code>, tương đương với <code>ep_size=tp_size, tp_plan={}</code>: chỉ | |
| dùng kế hoạch song song chuyên gia trên <code>tp_size</code> rank. Giá trị <code>ep_size</code> được chỉ định rõ ràng sẽ có ưu tiên cao hơn. Cờ này | |
| không thay đổi <code>tp_size</code> hoặc <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>tùy chọn</em>) — | |
| Số lượng thiết bị cho FSDP (song song dữ liệu). Nếu <code>None</code> và <code>tp_size</code> được đặt, mặc định là 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>tuỳ chọn</em>) — | |
| Số lượng bản sao FSDP (HSDP). Tham số được chia nhỏ trên <code>fsdp_size</code> thiết bị và được nhân bản | |
| <code>dp_replicate_size</code> lần: gradient được reduce-scatter trong một bản sao, sau đó được all-reduce giữa | |
| các bản sao. Mặc định là 1. Chúng tôi đề xuất sử dụng cho huấn luyện đa nút: FSDP thông thường sẽ all-gather tham số và | |
| reduce-scatter gradient trên tất cả các thiết bị ở mỗi bước, do đó lưu lượng này đi qua các liên kết | |
| giữa các nút chậm hơn. Đặt <code>dp_replicate_size</code> bằng số lượng nút và <code>fsdp_size * tp_size</code> bằng số | |
| thiết bị trên mỗi nút: lưu lượng chia nhỏ khi đó sẽ nằm trên các liên kết trong nút nhanh hơn, và chỉ có | |
| all-reduce gradient mới đi qua giữa các nút. Chi phí là bộ nhớ trên mỗi thiết bị tăng lên, vì tham số được chia nhỏ trong một nút.<!----></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>tùy chọn</em>) — | |
| Kế hoạch chia nhỏ FSDP2. Để <code>None</code> để sử dụng <code>base_model_fsdp_plan</code> được định nghĩa trước của mô hình và | |
| <code>_fsdp_plan</code> ở cấp lớp. Truyền một từ điển để ghi đè các quy tắc riêng lẻ; các quy tắc không được chỉ định sẽ được giữ nguyên. | |
| Chỉ được áp dụng khi <code>dp_replicate_size * fsdp_size > 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>tùy chọn</em>, mặc định là <code>False</code>) — | |
| Có bật offloading CPU cho FSDP2 hay không.<!----></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>tùy chọn</em>, mặc định là <code>False</code>) — | |
| Có bật độ chính xác hỗn hợp cho FSDP2 hay không.<!----></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>tùy chọn</em>) — | |
| Số lượng thiết bị cho pipeline parallelism. Nếu <code>None</code> và một chế độ song song khác được đặt, mặc định là 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>tùy chọn</em>) — | |
| Số lượng thiết bị sở hữu các shard chuyên gia riêng biệt. Mặc định là 1. Đặt giá trị này một cách rõ ràng để bật EP. Phải là | |
| bội số của <code>tp_size</code> và chia hết <code>fsdp_size * tp_size</code>. <code>ep_size > tp_size</code> cần một | |
| quy tắc <code>"ep_dispatch_experts"</code> với <code>ep_implementation</code> trao đổi token, chẳng hạn như <code>"all_to_all"</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>tuỳ chọn</em>) — | |
| Kế hoạch chia nhỏ song song chuyên gia. Để <code>None</code> để sử dụng <code>base_model_ep_plan</code> được định nghĩa trước của mô hình. Truyền một | |
| từ điển để ghi đè các quy tắc riêng lẻ của kế hoạch đó; các quy tắc không được chỉ định sẽ được giữ nguyên. Chỉ được áp dụng khi | |
| <code>ep_size > 1</code>, và các quy tắc của nó có ưu tiên cao hơn các quy tắc của <code>tp_plan</code> đối với cùng các module. Quy tắc | |
| <code>"ep_dispatch_experts"</code> cho phép <code>ep_implementation</code> chọn cách các token đến các chuyên gia.<!----></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>tuỳ chọn</em>, mặc định là <code>"auto"</code>) — | |
| Cách một quy tắc <code>"ep_dispatch_experts"</code> đưa các token đến cho các expert. Nó chỉ định một <code>EPImplementation</code> | |
| được đăng ký trong <code>EPImplementationInterface</code>: | |
| <ul> | |
| <li><code>"all_to_all"</code> trao đổi các token được định tuyến bằng các phép tập hợp all-to-all.</li> | |
| <li><code>"all_reduce"</code> chạy các chuyên gia của mỗi rank trên batch mà nhóm EP chia sẻ, rồi cộng các đầu ra. Nó yêu cầu <code>ep_size == tp_size</code>.</li> | |
| <li><code>"deepgemm_megamoe"</code> chuyển batch đó sang DeepGEMM Mega MoE, nơi kernel tự trao đổi các token. | |
| Việc nạp với <code>experts_implementation="deepgemm_megamoe"</code> sẽ chọn nó.</li> | |
| <li><code>"auto"</code> chạy <code>"all_reduce"</code> khi suy luận nếu nhóm EP chia sẻ một batch, và <code>"all_to_all"</code> trong các trường hợp khác.</li> | |
| </ul><!----></span></span></li><!--]--></ul><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--></div></div><!----> <p>Cấu hình cho suy luận và huấn luyện phân tán bản địa với song song tensor, pipeline hoặc 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:
- 82.6 kB
- Xet hash:
- 4715eb11c6d5d46b581efbd87216a0f813eebee65c78818949c0255e397745b4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.