Repository navigation
Gather expert/context co-sharded axes in FSDP weight prefetching (EP support) - #5848
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for gathering co-sharded axes (such as 'expert' and 'context') alongside FSDP axes during FSDP prefetching in MaxText. It adds helper functions _fsdp_axes_for_dim and _remove_fsdp_from_sharding to identify and strip these co-sharded axes from the sharding specifications, and updates the FSDP all-gather logic accordingly. Additionally, comprehensive unit tests have been added to verify the behavior for co-sharded, routed, and compound dimensions. No review comments were provided, and the implementation looks solid, so there is no additional feedback.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
71fc655 to
7859b98
Compare
Move FSDP_CO_AXES, fsdp_axes_for_dim, remove_fsdp_from_partition_spec, and
remove_fsdp_from_sharding to maxtext.utils.sharding so co-sharded axes
('context', 'context_usp_ulysses', 'expert') following an FSDP axis are
gathered alongside FSDP while preserving the MoE routed expert dimension.
7859b98 to
cab5380
Compare
Description
FSDP weight prefetching (
prefetch_fsdp_weights=True) did not prefetch most weights under expert parallelism.With
ici_expert_parallelism > 1, the non-routed weights (MLA attention, dense MLP, shared experts) are sharded over('fsdp', ..., 'expert')on their embed-like dims (embed,embed_attn,q_lora,kv_lorainbase.yml)._explicit_fsdp_all_gatheronly looked forfsdp/fsdp_transpose, so it requiredfsdpto be the minor-most axis of the dimension, foundexpertafter it, and returnedNone. That made it fall back to an untagged reshard overfsdp, and XLA then added a second, un-prefetched all-gather overexpertright before each consumer.This PR treats
expert,contextandcontext_usp_ulyssesas FSDP co-sharding axes (sharding.FSDP_CO_AXES) when they come after an FSDP axis on the same dimension:sharding.fsdp_axes_for_dim(entry): if a dimension has an FSDP axis, returns every co-sharding axis from the first FSDP axis onward.sharding.remove_fsdp_from_partition_spec()/sharding.remove_fsdp_from_sharding()strips those axes per dimension to get the gathered sharding, and_explicit_fsdp_all_gather()gathers them as onexla_explicit_fsdp-tagged all-gather (e.g. 128-way over('fsdp','expert')).'expert') and axes before the first FSDP axis (e.g.'expert'in('expert','fsdp')) keep their sharding, so routed experts stay EP-sharded and only gatherfsdp. Undercustom_mesh_and_rule=ep-as-dp, non-routed weights are already replicated over'expert'(so'expert'is absent from theirPartitionSpec), and routed MoE weights keep'expert'sharded.FSDP-only (
expert=1) is unchanged: size-1 axes are dropped before the gather axes are computed, and an EP=1 AoT compile gives the same set of tagged all-gathers (146 total / 144 tagged, identical shapes, axes and dims) before and after this change.Tests
Unit tests: 4 new tests in
tests/unit/fsdp_prefetch_test.py:remove_fsdp_from_sharding,('fsdp','expert')co-sharded dim gathered together, routed-expert'expert'dim preserved, and leading'expert'in a compound('expert','fsdp')dim preserved. All 12 tests pass.AoT compile (DSv3 15L/128E,
gf=4x4x4,fsdp=32 x expert=4): tagged FSDP all-gathers go from 32 to 144 (every decoder weight gather), and the 132 un-prefetched in-layerexpertgathers are gone.End-to-end, GhostFish 4x4x4: DSv3 15L, 256 experts,
ici_expert_parallelism=4,per_device_batch_size=1, top-k 4,remat_policy=custom; prefetch runs use--xla_tpu_enable_fsdp_latency_hiding_scheduler=true --xla_tpu_explicit_fsdp_dedicated_sparse_core_id=0 --xla_tpu_rerun_latency_hiding_scheduler_post_sc_assignment=false:EP_AS_FSDP,base.yml)EP_AS_DP,ep-as-dp.yml)use_ring_of_experts=True)The dedicated SparseCore is needed with EP. Without
xla_tpu_explicit_fsdp_dedicated_sparse_core_id, the prefetched gathers queue behind the layer's ragged-all-to-all (or Ring-of-Expertsall-gather+reduce-scatter) and other SparseCore work, and the step time is 1955.7 ms (-1.7%) (xid/301803674).Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.