Skip to content

[Module][Full DTensor] Config-based sharding for Qwen3, Llama4, DeepSeek V3, GPT-OSS - #2969

Merged
fegin merged 67 commits into
mainfrom
gh/fegin/109/head
Apr 27, 2026
Merged

fegin merged 67 commits into
mainfrom
gh/fegin/109/head

Conversation

@fegin

@fegin fegin commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Convert dense-layer TP and attention for all remaining models to config-based ShardingSpec, leaving MoE part unchanged, still using apply_moe_ep_tp(). After this PR, every model's attention uses Module.parallelize().

We will remove LocalInnerAttention in the next PR.
Models that are not converted: Flux and qwen3-vl.

  1. Flux doesn't support TP. So it is out of the current scope. We can convert it during the full dtensor PRs.
  2. qwen3-vl vision part is hard to convert without full dtensor support. It is doable but let's not do the hacky way and wait until the full dtensor PRs.

fegin added 2 commits April 14, 2026 15:14
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 14, 2026
…4, DeepSeek V3, GPT-OSS

Convert dense-layer TP and attention for all remaining models to config-based ShardingSpec, leaving MoE part unchanged, still using apply_moe_ep_tp(). After this PR, every model's attention uses Module.parallelize().

We will remove LocalInnerAttention in the next PR.


ghstack-source-id: f3d2786
Pull-Request: #2969
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 14, 2026
fegin added 2 commits April 14, 2026 15:25
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 14, 2026
…4, DeepSeek V3, GPT-OSS

Convert dense-layer TP and attention for all remaining models to config-based ShardingSpec, leaving MoE part unchanged, still using apply_moe_ep_tp(). After this PR, every model's attention uses Module.parallelize().

We will remove LocalInnerAttention in the next PR.

ghstack-source-id: 1343a6e
Pull-Request: #2969
[ghstack-poisoned]
fegin added 8 commits April 16, 2026 10:39
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin marked this pull request as ready for review April 17, 2026 16:51
fegin added 6 commits April 17, 2026 11:31
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't check actual plan

- apply training techniques in the following order
- TP (and EP if the model has MoE architecture)
- `model.parallelize(mesh)` — auto-recursive declarative sharding driven by `sharding_spec` (TP, SP, attention `local_map`). Replaces per-model `parallelize_module` plan dicts.
- (MoE models) `apply_moe_ep_tp` for expert-parallel + TP on MoE experts (not yet config-based).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now the support of EP + ETP should be poor in perf, any no one is doing that really. So I think we should

  • move the ETP > 1, EP=1 code to sharding.py
  • deprecate the ExpertTensorParallel class.
  • rename apply_moe_ep_tp to apply_ep, which itself should be killed after @acisseJZhong 's refactoring. The local_map application around GroupedExperts should also happen in set sharding.

Can be done in separate PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparse related change will come later. I only focus on the dense part. Mesh selection needs to be addressed first before I play with the sparse part.

fegin added 20 commits April 23, 2026 21:40
[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 requested a review from sanketpurandare as a code owner April 27, 2026 16:27
fegin added a commit that referenced this pull request Apr 27, 2026
…a3 adoption (#2963)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0)
(oldest at bottom):
* #2987
* #3113
* #3080
* #2986
* #2969
* __->__ #2963

**Summary**
Replace string-keyed plan dicts passed to `parallelize_module()` with
`ShardingSpec` attached to each `Module.Config`. Sharding is co-located
with the rest of a submodule's configuration and applied by
`Module.parallelize(mesh)` via auto-recursion.

**NOTE that we have to do some compromise designs due to the backward
compatibility issues, these should be removed either full DTensor is
applied or at some time in the future**

**Known TODOs for follow-up:**
   
- apply_cp_to_forward: This is a workaround to keep the current CP work.
Remove once all models use config-based sharding + full DTensor
- input_layouts: Another workaround. We can remove it once all inputs
flow as DTensors
- LocalMapInnerAttention: remove entire class once all models use
config-based sharding (#2986)
- parallelize(tp_mesh): pass full SPMD mesh once FSDP/CP also uses
DTensor (#2987)
- _shard_outputs: extend to support nested outputs (tuples/dicts) for
multi-tensor forward
@fegin
fegin changed the base branch from gh/fegin/109/base to main April 27, 2026 18:16
@fegin
fegin merged commit 8e82084 into main Apr 27, 2026
12 of 13 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Apr 28, 2026
…ytorch#2963/pytorch#2969)

Mirror upstream's llama3 sharding refactor for agpt. Replaces the manual
parallelize_module(...) + apply_tp(...) pipeline with the new
config-based sharding API: each sub-config gets a ShardingConfig
attached during update_from_config, and Module.parallelize(mesh) reads
those declarations at TP-application time.

New files:
- agpt/sharding.py: set_agpt_sharding_config — wrapper around upstream's
  set_decoder_sharding_config + set_gqa_attention_sharding +
  set_dense_ffn_sharding helpers, plus QK-Norm sharding (which upstream's
  GQA helper doesn't know about because llama3 has no QK-Norm).
- agpt/model.py: AgptModel(Llama3Model) — overrides Config.update_from_config
  to call set_agpt_sharding_config after the base llama3 setter runs.

Modified:
- agpt/__init__.py: switch all configs from Llama3Model.Config to
  AgptModel.Config so the ezpz update_from_config hook fires.
- agpt/parallelize.py: rewritten as a thin orchestrator. TP is now just
  model.parallelize(tp_mesh); apply_tp + parallelize_module + manual
  ColwiseParallel/RowwiseParallel/SequenceParallel/PrepareModuleInput
  plumbing all gone. Float8 tensorwise TP path also gone — the new
  sharding API doesn't have an equivalent yet, and we weren't using it
  in production. Will revisit when float8 lands in the new API upstream.

Preserved agpt-specific behavior:
- disable_fsdp_gradient_division still calls
  set_force_sum_reduction_for_comms(True) on non-NCCL backends (CCL/XPU).
- Post-apply_compile reset of capture_scalar_outputs=False to keep the
  separately-compiled CrossEntropyLoss working on dense models.
- apply_fsdp keeps the [norm, lm_head] joint grouping with
  reshard_after_forward gated on the policy.

Verified:
- agpt_configs['debugmodel'] / 2B / 2B_qknorm import + build_meta + run
  update_from_config + Module.parallelize(1D tp mesh) cleanly. lm_head
  becomes a DTensor as expected; QK-Norm RMSNorm gets a sharding_config.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Apr 28, 2026
…torch#2963/pytorch#2969)

Mirror upstream's deepseek_v3 sharding refactor for moe. The non-MoE TP
path (attention/norms/dense FFN) is now driven by sharding_config
declarations consumed by Module.parallelize(tp_mesh). MoE blocks are
still parallelized at parallelize-time by apply_moe_ep_tp — that mirrors
upstream deepseek_v3, where set_deepseek_v3_sharding_config also leaves
the MoE block alone.

New file:
- moe/sharding.py: set_moe_sharding_config — wrapper around upstream's
  set_decoder_sharding_config + set_dense_ffn_sharding helpers, plus the
  MLA-specific attention sharding. Binds against
  experiments.ezpz.moe.model.Attention (our MLA Attention is a separate
  class from upstream's, even though structurally identical).

Modified:
- moe/model.py: extends moeModel.Config.update_from_config to call
  set_moe_sharding_config after the existing rope/MoE sync logic.
- moe/parallelize.py: rewritten as a thin orchestrator. Drops 230+ lines
  of manual ColwiseParallel/RowwiseParallel/SequenceParallel/
  PrepareModuleInput plans (`apply_non_moe_tp`) that are now declared
  via sharding_config. Keeps the agpt-style XPU
  `disable_fsdp_gradient_division`, the per-block
  `block.compile(backend=...)` workaround (XPU can't fullgraph compile
  MoE routing), the inlined `apply_fsdp` (avoids ShardPlacementResult
  import that doesn't exist in Aurora's PyTorch) with the Shard(0)
  fallback for non-divisible expert hidden dims, and the
  `summarize_model` log line.

Verified locally on 1-rank gloo: imports clean, Module.parallelize(1D mesh)
populates DTensor on every sub-module (including lm_head, MLA wkv_a/wkv_b,
both q_lora_rank=0 and >0 paths). meta build of moe debugmodel succeeds
with the expected 1 dense + 5 MoE layer structure.
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.

2 participants