Expert-parallel implementations for `ep_dispatch_experts`: all-reduce at inference, all-to-all in training (#49429)
* build and expand once the tp/ep_plan
* unify
* clean fsdp
* reorganize to avoid breaking changes
* better distinction
* revert
* add tp plan to deepseek v4 (but attention stays replicated)
* Revert "add tp plan to deepseek v4 (but attention stays replicated)"
This reverts commit 3f1e4ab990df1fa64cb577695f6602e07610a5ab.
* Fix the merge of main: restore moved imports and the empty tp_plan override
* cleaner mixin
* cleaner
* deprecation cycle
* cleaner
* revert tp plan
* Pass the expanded plan in the bare-module colwise_gather_output test
* retrigger to verify fetcher
* the branch is now pointing to main and not to unify-resolve-plan
* Refactor expert parallelism registration and improve documentation
- Consolidated the registration of parallel styles into the `ParallelInterface` class for better organization.
- Updated the `expert_parallel.py` and `tensor_parallel.py` files to streamline the registration process of expert parallel strategies.
- Enhanced documentation for clarity regarding the usage of tensor parallel strategies and their registration.
- Removed redundant code related to `MoeExpertsParallel` and `MoeIdentityParallel` classes, which are now properly registered in the new structure.
* comment
* some models missed the tests EP mixin
* fix copyright
* move replace_layer_number_by_wildcard and _get_parameter_plan to parallel_utils
* Expert-parallel implementations for ep_dispatch_experts: all-reduce at inference, all-to-all in training
ep_dispatch_experts now reaches its experts through an EPImplementation picked by
DistributedConfig.ep_implementation from ALL_EP_IMPLEMENTATIONS:
- all_to_all: token dispatch, the previous behaviour.
- all_reduce: each rank masks the routes to other ranks' experts on the batch the EP
group shares and an all-reduce sums the outputs (ep_size == tp_size). No host sync,
so decode captures in a CUDA graph.
- auto (default): all_reduce at inference on a shared batch, all_to_all otherwise.
Built on the plan resolution of #49527.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Run the backward-only all-reduces only when a gradient can flow
One helper, _all_reduce_backward, applies _AllReduceBackward only when grad is
enabled and the input requires it, like the TP norm hooks (#49975). The MoE TP
experts, the MLA rope projection and the EP inputs no longer run it at inference
(~1.8us of host time per call in eager decode); the sites that already checked
use the helper too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Drop the config-side EP implementation resolver
ep_implementation defaults to "auto" instead of None. The lazy import of
_resolve_ep_implementation into DistributedConfig only re-checked what
install_forward (unknown name) and EPImplementation (shared batch) already
check, so it goes; install_forward raises the clear error for an unknown name.
EpDispatchExpertsParallel.should_use_local_tensors was no longer read.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Say what each EP implementation does in short sentences
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP implementations: one forward instead of dispatch + combine
`AllReduceEP` had nothing to dispatch and `AutoEP` bypassed both steps, so the
two-step contract didn't fit. An `EPImplementation` now implements `forward`
and runs the local experts with a shared `run_experts` (local DTensor params,
empty rows kept connected to backward). `AllToAllEP` keeps its two exchanges
as private `_send` / `_receive` steps.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP implementations are callables; inline the backward all-reduce check
Every `EPImplementation` defines `__call__` (AutoEP already did), and the base
class keeps `run_experts`, now owning the local DTensor params context.
`_all_reduce_backward` is gone: its call sites check
`torch.is_grad_enabled() and x.requires_grad` before `_AllReduceBackward.apply`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP: register implementations in a dict on EpDispatchExpertsParallel
- `EpDispatchExpertsParallel.ep_implementations` maps the three built-in
names to their classes, replacing `EPImplementationInterface` /
`ALL_EP_IMPLEMENTATIONS`; docs and the config docstring point to it.
- `AllToAllEP`'s halves are `_dispatch_tokens` and `_combine_tokens` again.
- `EpDispatchExpertsParallel.should_use_local_tensors` is back.
- Tests, from review: one parametrized check per implementation and mode
(train, inference, both checkpoint modes) against one process, a separate
check of AutoEP's choice, and install_forward's refusals.
Co-authored-by: Ferdinand Mom <47445085+3outeille@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP: inline the shared-batch check into EPImplementation
`_same_ranks` had one caller; the comparison of the TP and EP groups' ranks
now sits where `shares_batch` is set.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP: always take the TP mesh in EP implementations
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP: helper for the gradient-only all-reduce, kwarg ep mesh, one toy experts class in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP: grad check inside _AllReduceBackward.apply, EPImplementationInterface registry, doc notes
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* EP: local params around the implementation call in `ep_forward`; empty-rows case in `AllToAllEP`
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: 3outeille <ferdinand.mom@epita.fr>
Co-authored-by: Ferdinand Mom <47445085+3outeille@users.noreply.github.com>
Co-authored-by: Tarek Ziade <tarek@ziade.org>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>