Skip to content

[NOT READY FOR REVIEW][Full DTensor] Enable full_dtensor for MoE models (qwen3, llama4, gpt_oss, deepseek_v3) - #3161

Closed
fegin wants to merge 21 commits into
gh/fegin/119/basefrom
gh/fegin/119/head
Closed

fegin wants to merge 21 commits into
gh/fegin/119/basefrom
gh/fegin/119/head

Conversation

@fegin

@fegin fegin commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Drops the EP gate in full_dtensor.validate_config and adds the sparse
SPMD mesh wiring needed for routed-expert FSDP under
--training.full_dtensor. All four MoE models now support full DTensor
mode where dense subtree sits on the dense SPMD mesh
(dp_replicate, dp_shard, cp, tp) and routed experts on the sparse
SPMD mesh (dp_replicate, efsdp, ep, etp), both within one
model.parallelize(parallel_dims) invocation.

Verified at 500 steps EP=2 (4xH100, --debug.seed=42):

  • qwen3: loss 2.00980, grad_norm 0.5127
  • llama4: loss 1.81126, grad_norm 0.1787
  • gpt_oss: loss 1.90610, grad_norm 0.2511
  • deepseek_v3: loss 2.02116, grad_norm 0.2729

Non-full_dtensor regression bit-identical to PR7 across all four models.

Sparse mesh helpers in full_dtensor.py

  • _sparse_spmd_axes(parallel_dims) -- canonical sparse SPMD axis
    list, intersected with enabled axes. Sibling of _dense_spmd_axes.
  • get_sparse_dp_mesh_axes(parallel_dims) -- DataParallelMeshDims
    for routed-expert FSDP: shard="efsdp", replicate="dp_replicate"
    when enabled. Sibling of get_dp_mesh_axes.
  • resolve_sparse_fsdp_mesh(parallel_dims, full_dtensor) -- returns
    (sparse_fsdp_mesh, sparse_dp_mesh_dims) under full_dtensor; under
    non-full_dtensor returns the legacy efsdp / (dp_replicate, efsdp) mesh and None for the dims. Returns (None, None)
    when EP is disabled. Sibling of resolve_fsdp_mesh.
  • validate_config -- drops the
    if parallel_dims.ep_enabled: raise NotImplementedError gate. CP +
    SDPA/Varlen rejection retained.

apply_fsdp mesh routing under full_dtensor (llama4/parallelize.py)

Adds dp_mesh_dims and edp_mesh_dims kwargs. Plumbed through
fsdp_config for the outer fully_shard call and into
FSDPMeshInfo(spmd_mesh=, dp_mesh_dims=) for per-param routing under
EP > 1. The existing _shard_placement_fn shape (per-param
param in expert_params check) stays; only the mesh objects swap.

Per-model parallelize_*.py enable

For qwen3 / llama4 / gpt_oss / deepseek_v3, replaces

if parallelism.full_dtensor:
    raise NotImplementedError("full_dtensor is not supported yet.")

with the llama3 pattern:

if parallelism.full_dtensor:
    validate_config(parallel_dims, model)
    model.parallelize(parallel_dims)
else:
    if parallel_dims.cp_enabled:
        apply_cp_to_forward(...)
    if parallel_dims.tp_enabled or parallel_dims.ep_enabled:
        model.parallelize(parallel_dims)

and resolves both the dense and sparse FSDP meshes via the new helpers
under full_dtensor.

Module.parallelize partition check (subset, not strict equality)

Under full_dtensor, the partition check that ensures
distribute_tensor reaches every SPMD peer rank is relaxed from
strict equality to subset. LocalMap-only configs (e.g. inner
attention's {TP: Shard(2)}) declare a narrow axis subset of the
dense partition without claiming coverage of the full mesh. State-
shardings helpers always emit all dense axes so they pass either way.
Empty declared (every axis disabled in this run) is treated as a no-op,
not an invalid partition.

set_gqa_inner_attention_local_map: full dense axis set

Switched qkv_placements from bare {TP: Shard(2)} to
dense_activation_placement(tp=Shard(2)). Under full_dtensor where
the surrounding mesh is multi-axis (e.g. (dp_shard, tp)),
local_map's redistribute requires placements for every mesh axis;
the bare {TP: ...} form crashed with "list index out of range" in
generate_greedy_transform_infos. Under non-full_dtensor the
(tp,)-only mesh only consumes the TP placement and the rest are
ignored.

set_moe_sharding_config: dense submodules unconditional

The MoE wrapper, router gate, and shared experts had their
sharding_config set only when tp_enabled. Under full_dtensor +
EP-only (TP=1), this left those dense params plain (not DTensor),
which fired FSDP2's invariant
"all parameters must be DTensors on the full SPMD mesh." Now set
unconditionally. Under TP=1 the TP-axis placements get filtered at
resolve_placements time and the configs become all-Replicate on
remaining dense axes (no-op for parallelism, but the params are
DTensors as required).

Out of scope

  • qwen3_vl (deferred to PR9, full conversion + full_dtensor).
  • vLLM RL inference path.
  • Final deletion of apply_moe_ep_tp from llama4/parallelize.py
    (still needed by qwen3_vl until PR9).

[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 29, 2026
fegin added a commit that referenced this pull request Apr 29, 2026
…ma4, gpt_oss, deepseek_v3)

Drops the EP gate in ``full_dtensor.validate_config`` and adds the sparse
SPMD mesh wiring needed for routed-expert FSDP under
``--training.full_dtensor``. All four MoE models now support full DTensor
mode where dense subtree sits on the dense SPMD mesh
``(dp_replicate, dp_shard, cp, tp)`` and routed experts on the sparse
SPMD mesh ``(dp_replicate, efsdp, ep, etp)``, both within one
``model.parallelize(parallel_dims)`` invocation.

Verified at 500 steps EP=2 (4xH100, ``--debug.seed=42``):

  - qwen3:       loss 2.00980, grad_norm 0.5127
  - llama4:      loss 1.81126, grad_norm 0.1787
  - gpt_oss:     loss 1.90610, grad_norm 0.2511
  - deepseek_v3: loss 2.02116, grad_norm 0.2729

Non-full_dtensor regression bit-identical to PR7 across all four models.

## Sparse mesh helpers in ``full_dtensor.py``

- ``_sparse_spmd_axes(parallel_dims)`` -- canonical sparse SPMD axis
  list, intersected with enabled axes. Sibling of ``_dense_spmd_axes``.
- ``get_sparse_dp_mesh_axes(parallel_dims)`` -- ``DataParallelMeshDims``
  for routed-expert FSDP: ``shard="efsdp"``, ``replicate="dp_replicate"``
  when enabled. Sibling of ``get_dp_mesh_axes``.
- ``resolve_sparse_fsdp_mesh(parallel_dims, full_dtensor)`` -- returns
  ``(sparse_fsdp_mesh, sparse_dp_mesh_dims)`` under full_dtensor; under
  non-full_dtensor returns the legacy ``efsdp`` / ``(dp_replicate,
  efsdp)`` mesh and ``None`` for the dims. Returns ``(None, None)``
  when EP is disabled. Sibling of ``resolve_fsdp_mesh``.
- ``validate_config`` -- drops the
  ``if parallel_dims.ep_enabled: raise NotImplementedError`` gate. CP +
  SDPA/Varlen rejection retained.

## ``apply_fsdp`` mesh routing under full_dtensor (llama4/parallelize.py)

Adds ``dp_mesh_dims`` and ``edp_mesh_dims`` kwargs. Plumbed through
``fsdp_config`` for the outer ``fully_shard`` call and into
``FSDPMeshInfo(spmd_mesh=, dp_mesh_dims=)`` for per-param routing under
EP > 1. The existing ``_shard_placement_fn`` shape (per-param
``param in expert_params`` check) stays; only the mesh objects swap.

## Per-model ``parallelize_*.py`` enable

For qwen3 / llama4 / gpt_oss / deepseek_v3, replaces

    if parallelism.full_dtensor:
        raise NotImplementedError("full_dtensor is not supported yet.")

with the llama3 pattern:

    if parallelism.full_dtensor:
        validate_config(parallel_dims, model)
        model.parallelize(parallel_dims)
    else:
        if parallel_dims.cp_enabled:
            apply_cp_to_forward(...)
        if parallel_dims.tp_enabled or parallel_dims.ep_enabled:
            model.parallelize(parallel_dims)

and resolves both the dense and sparse FSDP meshes via the new helpers
under full_dtensor.

## Module.parallelize partition check (subset, not strict equality)

Under full_dtensor, the partition check that ensures
``distribute_tensor`` reaches every SPMD peer rank is relaxed from
strict equality to subset. ``LocalMap``-only configs (e.g. inner
attention's ``{TP: Shard(2)}``) declare a narrow axis subset of the
dense partition without claiming coverage of the full mesh. State-
shardings helpers always emit all dense axes so they pass either way.
Empty declared (every axis disabled in this run) is treated as a no-op,
not an invalid partition.

## ``set_gqa_inner_attention_local_map``: full dense axis set

Switched ``qkv_placements`` from bare ``{TP: Shard(2)}`` to
``dense_activation_placement(tp=Shard(2))``. Under full_dtensor where
the surrounding mesh is multi-axis (e.g. ``(dp_shard, tp)``),
``local_map``'s redistribute requires placements for every mesh axis;
the bare ``{TP: ...}`` form crashed with "list index out of range" in
``generate_greedy_transform_infos``. Under non-full_dtensor the
(tp,)-only mesh only consumes the ``TP`` placement and the rest are
ignored.

## ``set_moe_sharding_config``: dense submodules unconditional

The MoE wrapper, router gate, and shared experts had their
``sharding_config`` set only when ``tp_enabled``. Under full_dtensor +
EP-only (TP=1), this left those dense params plain (not DTensor),
which fired FSDP2's invariant
"all parameters must be DTensors on the full SPMD mesh." Now set
unconditionally. Under TP=1 the TP-axis placements get filtered at
``resolve_placements`` time and the configs become all-Replicate on
remaining dense axes (no-op for parallelism, but the params are
DTensors as required).

## Out of scope

- qwen3_vl (deferred to PR9, full conversion + full_dtensor).
- vLLM RL inference path.
- Final deletion of ``apply_moe_ep_tp`` from ``llama4/parallelize.py``
  (still needed by qwen3_vl until PR9).


ghstack-source-id: c12eb04
Pull-Request: #3161
@fegin fegin changed the title [Module][Full DTensor] Enable full_dtensor for MoE models (qwen3, llama4, gpt_oss, deepseek_v3) [NOT READY FOR REVIEW][Module][Full DTensor] Enable full_dtensor for MoE models (qwen3, llama4, gpt_oss, deepseek_v3) Apr 29, 2026
@fegin fegin changed the title [NOT READY FOR REVIEW][Module][Full DTensor] Enable full_dtensor for MoE models (qwen3, llama4, gpt_oss, deepseek_v3) [NOT READY FOR REVIEW][Full DTensor] Enable full_dtensor for MoE models (qwen3, llama4, gpt_oss, deepseek_v3) Apr 29, 2026
fegin added 20 commits April 29, 2026 17:51
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin fegin closed this May 18, 2026
@fegin fegin reopened this May 18, 2026
@fegin fegin closed this May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant