Skip to content

[Module][Full DTensor] Config-based sharding infrastructure with llama3 adoption - #2963

Merged
fegin merged 48 commits into
mainfrom
gh/fegin/108/head
Apr 27, 2026
Merged

fegin merged 48 commits into
mainfrom
gh/fegin/108/head

Conversation

@fegin

@fegin fegin commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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:

fegin added 2 commits April 14, 2026 10:04
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 14, 2026
…ith llama3 adoption

Replace imperative parallelize_module() TP plans with declarative ShardingSpec on Module.Config for Llama3. This PR also ensures that DTensor for TP is used for llama3 (no to local).


ghstack-source-id: c31b7f8
Pull-Request: #2963
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 14, 2026
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 14, 2026
…ith llama3 adoption

Replace imperative parallelize_module() TP plans with declarative ShardingSpec on Module.Config for Llama3. This PR also ensures that DTensor for TP is used for llama3 (no to local).

ghstack-source-id: 4d452cb
Pull-Request: #2963
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 14, 2026
…ith llama3 adoption

Replace imperative parallelize_module() TP plans with declarative ShardingSpec on Module.Config for Llama3. This PR also ensures that DTensor for TP is used for llama3 (no to local).

ghstack-source-id: 0390812
Pull-Request: #2963
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 14, 2026
…ith llama3 adoption

Replace imperative parallelize_module() TP plans with declarative ShardingSpec on Module.Config for Llama3. This PR also ensures that DTensor for TP is used for llama3 (no to local).

Known TODOs for follow-up:
- apply_cp_to_forward: remove once all models use config-based sharding + full DTensor
- LocalMapInnerAttention: remove entire class once all models use config-based sharding
- parallelize(tp_mesh): pass full world mesh once FSDP/CP also use DTensor
- input_layouts: remove once all inputs flow as DTensors
- _shard_outputs: extend to support nested outputs (tuples/dicts) for multi-tensor forward

ghstack-source-id: 0524554
Pull-Request: #2963
[ghstack-poisoned]
Comment thread torchtitan/protocols/sharding.py Resolved
Comment thread torchtitan/protocols/sharding.py Resolved
fegin added 2 commits April 16, 2026 10:39
[ghstack-poisoned]
[ghstack-poisoned]
Comment thread torchtitan/distributed/sharding.py Resolved
Comment thread torchtitan/distributed/sharding.py Resolved
Comment thread torchtitan/models/llama3/sharding.py Resolved
Comment thread torchtitan/models/llama3/sharding.py Resolved
Comment thread torchtitan/models/llama3/sharding.py Resolved
Comment thread torchtitan/protocols/model_spec.py Resolved
Comment thread torchtitan/protocols/model_spec.py Resolved
fegin added 3 commits April 16, 2026 14:26
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin marked this pull request as ready for review April 17, 2026 16:51
[ghstack-poisoned]
Comment thread torchtitan/protocols/sharding.py Resolved
fegin added 9 commits April 24, 2026 10:49
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 24, 2026
…ith llama3 adoption

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. Llama3 is migrated in this
PR; remaining models follow in the next PR. Also ensures that TP uses
DTensor end-to-end for Llama3 (no to_local() at module boundaries).

Known TODOs for follow-up:
- apply_cp_to_forward: remove once all models use config-based sharding + full DTensor
- LocalMapInnerAttention: remove entire class once all models use config-based sharding
- parallelize(tp_mesh): pass full world mesh once FSDP/CP also use DTensor
- input_layouts: remove once all inputs flow as DTensors
- _shard_outputs: extend to support nested outputs (tuples/dicts) for multi-tensor forward

ghstack-source-id: 8c2950f
Pull-Request: #2963
fegin added 2 commits April 24, 2026 15:06
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 24, 2026
…ith llama3 adoption

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. Llama3 is migrated in this
PR; remaining models follow in the next PR. Also ensures that TP uses
DTensor end-to-end for Llama3 (no to_local() at module boundaries).

Known TODOs for follow-up:
- apply_cp_to_forward: remove once all models use config-based sharding + full DTensor
- LocalMapInnerAttention: remove entire class once all models use config-based sharding
- parallelize(tp_mesh): pass full world mesh once FSDP/CP also use DTensor
- input_layouts: remove once all inputs flow as DTensors
- _shard_outputs: extend to support nested outputs (tuples/dicts) for multi-tensor forward

ghstack-source-id: 10fb152
Pull-Request: #2963
[ghstack-poisoned]
fegin added a commit that referenced this pull request Apr 24, 2026
…ith llama3 adoption

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. Llama3 is migrated in this
PR; remaining models follow in the next PR. Also ensures that TP uses
DTensor end-to-end for Llama3 (no to_local() at module boundaries).

Known TODOs for follow-up:
- apply_cp_to_forward: remove once all models use config-based sharding + full DTensor
- LocalMapInnerAttention: remove entire class once all models use config-based sharding
- parallelize(tp_mesh): pass full world mesh once FSDP/CP also use DTensor
- input_layouts: remove once all inputs flow as DTensors
- _shard_outputs: extend to support nested outputs (tuples/dicts) for multi-tensor forward

ghstack-source-id: 5d896ab
Pull-Request: #2963
fegin added 3 commits April 24, 2026 15:40
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@fegin
fegin requested a review from sanketpurandare as a code owner April 27, 2026 16:27
@fegin fegin added the ciflow/h100.8 Trigger H100.8 CI label Apr 27, 2026
@fegin
fegin changed the base branch from gh/fegin/108/base to main April 27, 2026 18:15
@fegin
fegin merged commit 1786292 into main Apr 27, 2026
14 of 16 checks passed
fegin added a commit that referenced this pull request Apr 27, 2026
…eek V3, GPT-OSS (#2969)

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

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.
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.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Apr 28, 2026
50-step moe smoke test using the smallest non-debug flavor (500M) on
2 nodes, AdamW, no checkpoint, fineweb-edu HF stream. Mirrors
smoke_2b_50steps for moe to verify the post-pytorch#2963/pytorch#2937 replay end-to-end:
imports, model build, sharding-config population on MLA attention + dense
FFN, Module.parallelize, apply_moe_ep_tp, per-block compile, FSDP wrap,
optimizer step, loss decreasing.

scripts/submit_moe_smoke.sh mirrors competition/submit_run.sh but for
the moe module path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI 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.

3 participants