Skip to content

[spmd_types] Overlap module-boundary collectives with compute (async_op) - #5398

Merged
pianpwk merged 4 commits into
mainfrom
gh/pianpwk/114/head
Oct 15, 2026
Merged

pianpwk merged 4 commits into
mainfrom
gh/pianpwk/114/head

Conversation

@pianpwk

@pianpwk pianpwk commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

spmd_types collectives wait on the NCCL result right after issuing it, so
module-boundary redistributes (e.g. the sequence-parallel all-gather before
attention/MLP and the reduce-scatter after) never overlap with compute in
eager mode. The DTensor backend avoided this by calling
redistribute(..., async_op=True) at exactly these boundaries.

spmd_types now has the same opt-in (meta-pytorch/spmd_types@fce1acb:
async_op, forward only by default; backward_options={"async_op": True}
for backward). This bumps the spmd_types pin to that commit and passes
async_op=True through spmd_redistribute_per_axis from the two
module-boundary call sites in Module. Backward collectives still wait.

[ghstack-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests labels Oct 14, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 14, 2026
pianpwk added a commit that referenced this pull request Oct 14, 2026
spmd_types collectives wait on the NCCL result right after issuing it, so
module-boundary redistributes (e.g. the sequence-parallel all-gather before
attention/MLP and the reduce-scatter after) never overlap with compute in
eager mode. The DTensor backend avoided this by calling
`redistribute(..., async_op=True)` at exactly these boundaries.

spmd_types now has the same opt-in (meta-pytorch/spmd_types@fce1acb:
`async_op`, forward only by default; `backward_options={"async_op": True}`
for backward). This bumps the spmd_types pin to that commit and passes
`async_op=True` through `spmd_redistribute_per_axis` from the two
module-boundary call sites in `Module`. Backward collectives still wait.

ghstack-source-id: 1625e9d
Pull Request resolved: #5398
Comment thread torchtitan/distributed/spmd_types.py Outdated
src: spmd.SpmdType,
dst: spmd.SpmdType,
*,
async_op: bool = False,

@tianyu-l tianyu-l Oct 15, 2026 •

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.

probably shouldn't have a default, to encourage explicit behavior

[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Oct 15, 2026
spmd_types collectives wait on the NCCL result right after issuing it, so
module-boundary redistributes (e.g. the sequence-parallel all-gather before
attention/MLP and the reduce-scatter after) never overlap with compute in
eager mode. The DTensor backend avoided this by calling
`redistribute(..., async_op=True)` at exactly these boundaries.

spmd_types now has the same opt-in (meta-pytorch/spmd_types@fce1acb:
`async_op`, forward only by default; `backward_options={"async_op": True}`
for backward). This bumps the spmd_types pin to that commit and passes
`async_op=True` through `spmd_redistribute_per_axis` from the two
module-boundary call sites in `Module`. Backward collectives still wait.

ghstack-source-id: 36cf94a
Pull Request resolved: #5398
@pianpwk
pianpwk changed the base branch from gh/pianpwk/114/base to main October 15, 2026 20:31
@pianpwk
pianpwk merged commit 40c3d77 into main Oct 15, 2026
anijain2305 added a commit that referenced this pull request Nov 4, 2026
The token embedding output and the final (pre-lm_head) norm output were the
last decoder collectives still inserted implicitly by ShardingConfig
src -> dst redistribution. This moves them into model code, following the
pattern of RowParallelLinear / ColumnParallelLinear and the MoE change (#4878).

Embedding:
- Add VocabParallelEmbedding(Embedding). The masked vocab-shard lookup runs in
  an explicit spmd.local_map region that returns TP-Partial; the module then
  reduces it in the remat region `<fqn>.tp_reduce`:
  P -> S(0) (reduce-scatter) with SP, P -> I (all-reduce) without.
- `replicate_output=True` selects P -> R instead, for multimodal models whose
  vision scatter needs the full sequence on every TP rank (qwen3_5 family,
  kimi_k3, and kimi_k2_7 under SP).
- The base Embedding keeps returning TP-Partial. The Transformers modeling
  backend experiment converts nn.Embedding to it in place and still owns its
  own reduction; it is migrated separately.

Final norm:
- lm_head is vocab-sharded (column-parallel), so it needs the full hidden
  states on every TP rank, while the final norm sees them in the residual
  layout. On main, pre_lm_head_norm_sharding_config's out_dst=R hid that
  conversion.
- Add Decoder._final_norm_and_gather: the root norm followed by
  maybe_gather_tp_input, S(0) -> R (all-gather) with SP and I -> R (forward
  no-op, backward all-reduce) without. The gather runs after the norm, so
  under SP the norm only touches the local sequence shard, and it runs
  once, not inside lm_head, which ChunkedLossWrapper calls once per chunk.
- Decoder and every model with its own forward (deepseek_v3 MTP,
  deepseek_v4, kimi_k2_7, kimi_k3, muse_glimmer, muse_spark, qwen3_5) call
  it. The deepseek_v3 MTP block gathers its mtp_norm output the same way.

token_embedding_sharding_config, multimodal_token_embedding_sharding_config,
and pre_lm_head_norm_sharding_config now only declare the boundary layouts
(asserted under typechecking) and no longer request a redistribution.

BC: custom models that build Embedding.Config with
token_embedding_sharding_config must switch to VocabParallelEmbedding.Config,
and custom forwards that call the root norm must call
_final_norm_and_gather (or add maybe_gather_tp_input) instead.

Bug fix (Muse Spark): on main, Muse Spark's token embedding receives no
gradient under TP. The implicit boundary redistribution runs with
async_op=True (#5398) and returns an AsyncCollectiveTensor; it flows straight
into the compiled `muse_spark_rmsnorm` region, and torch.compile returns an
output with requires_grad=False, silently cutting the graph. The explicit
reduction here is synchronous like RowParallelLinear's, so the embedding trains
again. Verified with seed 42: at TP2+EP2 every step-1 parameter gradient is
bitwise identical to main except tok_embeddings.embedding.weight (None on main,
0.0590 here; 0.0564 at TP1). The async/compile interaction itself is an
upstream issue; this change only stops exercising it at these boundaries.

Numerics: bitwise-identical loss and grad_norm over 10 steps vs. main with
deterministic seed 42 on llama3 (TP2+SP, TP2 no-SP, TP2+CP2), qwen3 (TP2+SP),
deepseek_v3 with MTP (TP2+EP2, SP and no-SP), gpt_oss (TP2+EP2), and
deepseek_v4 (FSDP). Muse Spark differs only as described above. qwen3_5,
kimi_k2_7, kimi_k3, and Muse Glimmer multimodal could not be compared on the
test machine (base-tree env/recipe failures or nondeterministic kernels).
anijain2305 added a commit that referenced this pull request Nov 6, 2026
The token embedding output and the final (pre-lm_head) norm output were the
last decoder collectives still inserted implicitly by ShardingConfig
src -> dst redistribution. This moves them into model code, following the
pattern of RowParallelLinear / ColumnParallelLinear and the MoE change (#4878).

Embedding:
- Rename Embedding to VocabParallelEmbedding (the TODO on main) and make it the
  only token embedding class, so no embedding can return an unreduced TP
  partial. Under TP, the masked vocab-shard lookup runs in a spmd.local_map
  region (TP-replicated token ids in, TP-partial embeddings out), then the
  module reduces to the residual layout: P -> S(0) (reduce-scatter) with SP,
  P -> I (all-reduce) without. Without TP it is a plain lookup.
- The embedding always emits the residual layout. Multimodal models whose
  vision scatter needs the full sequence on every TP rank gather explicitly
  before it with the new multimodal.residual_to_replicated_layout, under the
  same conditions that previously selected a replicated embedding: Qwen3.5
  family and Kimi K3 when they have a vision encoder, Kimi K2.5 when it has
  one and SP is on. multimodal_token_embedding_sharding_config is removed.
- The Transformers modeling backend keeps its (batch, sequence) layout and its
  own reduction through a lookup-only subclass in module_conversion.py.

Final norm:
- lm_head is vocab-sharded (column-parallel), so it needs the full hidden
  states on every TP rank, while the final norm sees them in the residual
  layout. On main, pre_lm_head_norm_sharding_config's out_dst=R hid that
  conversion.
- Add Decoder._final_norm_and_gather: the root norm followed by
  maybe_gather_tp_input, S(0) -> R (all-gather) with SP and I -> R (forward
  no-op, backward all-reduce) without. The gather runs after the norm, so
  under SP the norm only touches the local sequence shard, and it runs
  once, not inside lm_head, which ChunkedLossWrapper calls once per chunk.
- Decoder and every model with its own forward (deepseek_v3 MTP,
  deepseek_v4, kimi_k2_7, kimi_k3, muse_glimmer, muse_spark, qwen3_5) call
  it. The deepseek_v3 MTP block gathers its mtp_norm output the same way.

token_embedding_sharding_config and pre_lm_head_norm_sharding_config now only
declare the boundary layouts (asserted under typechecking) and no longer
request a redistribution.

BC: Embedding is renamed to VocabParallelEmbedding; custom forwards that call
the root norm before lm_head must call _final_norm_and_gather (or add
maybe_gather_tp_input) instead.

Bug fix (Muse Spark): on main, Muse Spark's token embedding receives no
gradient under TP. The implicit boundary redistribution runs with
async_op=True (#5398) and returns an AsyncCollectiveTensor; it flows straight
into the compiled `muse_spark_rmsnorm` region, and torch.compile returns an
output with requires_grad=False, silently cutting the graph. The explicit
reduction here is synchronous like RowParallelLinear's, so the embedding trains
again. Verified with seed 42: at TP2+EP2 every step-1 parameter gradient is
bitwise identical to main except tok_embeddings.embedding.weight (None on main,
0.0590 here; 0.0564 at TP1). The async/compile interaction itself is an
upstream issue; this change only stops exercising it at these boundaries.

Numerics: bitwise-identical loss and grad_norm over 10 steps vs. main with
deterministic seed 42 on llama3 (TP2+SP, TP2 no-SP, TP2+CP2), qwen3 (TP2+SP),
deepseek_v3 with MTP (TP2+EP2, SP and no-SP), gpt_oss (TP2+EP2), and
deepseek_v4 (FSDP). Multimodal flavors (Qwen3.5 and Kimi K2.5 debug models,
which build a vision encoder) under TP2 with and without SP: full-model fwd+bwd
output and every parameter gradient bitwise identical to main (main itself
deterministic across two runs). Muse Spark differs only as described above.
anijain2305 added a commit that referenced this pull request Nov 6, 2026
The token embedding output and the final (pre-lm_head) norm output were the
last decoder collectives still inserted implicitly by ShardingConfig
src -> dst redistribution. This moves them into model code, following the
pattern of RowParallelLinear / ColumnParallelLinear and the MoE change (#4878).

Embedding:
- Rename Embedding to VocabParallelEmbedding (the TODO on main) and make it the
  only token embedding class, so no embedding can return an unreduced TP
  partial. Under TP, the masked vocab-shard lookup runs in a spmd.local_map
  region (TP-replicated token ids in, TP-partial embeddings out), then the
  module reduces to the residual layout: P -> S(0) (reduce-scatter) with SP,
  P -> I (all-reduce) without. Without TP it is a plain lookup.
- The embedding always emits the residual layout. Multimodal models whose
  vision scatter needs the full sequence on every TP rank gather explicitly
  before it with the new multimodal.residual_to_replicated_layout, under the
  same conditions that previously selected a replicated embedding: Qwen3.5
  family and Kimi K3 when they have a vision encoder, Kimi K2.5 when it has
  one and SP is on. multimodal_token_embedding_sharding_config is removed.
- The Transformers modeling backend keeps its (batch, sequence) layout and its
  own reduction through a lookup-only subclass in module_conversion.py.

Final norm:
- lm_head is vocab-sharded (column-parallel), so it needs the full hidden
  states on every TP rank, while the final norm sees them in the residual
  layout. On main, pre_lm_head_norm_sharding_config's out_dst=R hid that
  conversion.
- Add Decoder._pre_lm_head_norm_and_gather: the root norm followed by
  maybe_gather_tp_input, S(0) -> R (all-gather) with SP and I -> R (forward
  no-op, backward all-reduce) without. The gather runs after the norm, so
  under SP the norm only touches the local sequence shard, and it runs
  once, not inside lm_head, which ChunkedLossWrapper calls once per chunk.
- Decoder and every model with its own forward (deepseek_v3 MTP,
  deepseek_v4, kimi_k2_7, kimi_k3, muse_glimmer, muse_spark, qwen3_5) call
  it. The deepseek_v3 MTP block gathers its mtp_norm output the same way.

token_embedding_sharding_config and pre_lm_head_norm_sharding_config now only
declare the boundary layouts (asserted under typechecking) and no longer
request a redistribution.

BC: Embedding is renamed to VocabParallelEmbedding; custom forwards that call
the root norm before lm_head must call _pre_lm_head_norm_and_gather (or add
maybe_gather_tp_input) instead.

Bug fix (Muse Spark): on main, Muse Spark's token embedding receives no
gradient under TP. The implicit boundary redistribution runs with
async_op=True (#5398) and returns an AsyncCollectiveTensor; it flows straight
into the compiled `muse_spark_rmsnorm` region, and torch.compile returns an
output with requires_grad=False, silently cutting the graph. The explicit
reduction here is synchronous like RowParallelLinear's, so the embedding trains
again. Verified with seed 42: at TP2+EP2 every step-1 parameter gradient is
bitwise identical to main except tok_embeddings.embedding.weight (None on main,
0.0590 here; 0.0564 at TP1). The async/compile interaction itself is an
upstream issue; this change only stops exercising it at these boundaries.

Numerics: bitwise-identical loss and grad_norm over 10 steps vs. main with
deterministic seed 42 on llama3 (TP2+SP, TP2 no-SP, TP2+CP2), qwen3 (TP2+SP),
deepseek_v3 with MTP (TP2+EP2, SP and no-SP), gpt_oss (TP2+EP2), and
deepseek_v4 (FSDP). Multimodal flavors (Qwen3.5 and Kimi K2.5 debug models,
which build a vision encoder) under TP2 with and without SP: full-model fwd+bwd
output and every parameter gradient bitwise identical to main (main itself
deterministic across two runs). Muse Spark differs only as described above.
anijain2305 added a commit that referenced this pull request Nov 6, 2026
The token embedding output and the final (pre-lm_head) norm output were the
last decoder collectives still inserted implicitly by ShardingConfig
src -> dst redistribution. This moves them into model code, following the
pattern of RowParallelLinear / ColumnParallelLinear and the MoE change (#4878).

Embedding:
- Rename Embedding to VocabParallelEmbedding (the TODO on main) and make it the
  only token embedding class, so no embedding can return an unreduced TP
  partial. Under TP, the masked vocab-shard lookup runs in a spmd.local_map
  region (TP-replicated token ids in, TP-partial embeddings out), then the
  module reduces to the residual layout: P -> S(0) (reduce-scatter) with SP,
  P -> I (all-reduce) without. Without TP it is a plain lookup.
- The embedding always emits the residual layout. Multimodal models whose
  vision scatter needs the full sequence on every TP rank gather explicitly
  before it with the new multimodal.residual_to_replicated_layout, under the
  same conditions that previously selected a replicated embedding: Qwen3.5
  family and Kimi K3 when they have a vision encoder, Kimi K2.5 when it has
  one and SP is on. multimodal_token_embedding_sharding_config is removed.
- The Transformers modeling backend keeps its (batch, sequence) layout and its
  own reduction through a lookup-only subclass in module_conversion.py.

Final norm:
- lm_head is vocab-sharded (column-parallel), so it needs the full hidden
  states on every TP rank, while the final norm sees them in the residual
  layout. On main, pre_lm_head_norm_sharding_config's out_dst=R hid that
  conversion.
- Add Decoder._pre_lm_head_norm_and_gather: the root norm followed by
  maybe_gather_tp_input, S(0) -> R (all-gather) with SP and I -> R (forward
  no-op, backward all-reduce) without. The gather runs after the norm, so
  under SP the norm only touches the local sequence shard, and it runs
  once, not inside lm_head, which ChunkedLossWrapper calls once per chunk.
- Decoder and every model with its own forward (deepseek_v3 MTP,
  deepseek_v4, kimi_k2_7, kimi_k3, muse_glimmer, muse_spark, qwen3_5) call
  it. The deepseek_v3 MTP block gathers its mtp_norm output the same way.

token_embedding_sharding_config and pre_lm_head_norm_sharding_config now only
declare the boundary layouts (asserted under typechecking) and no longer
request a redistribution.

BC: Embedding is renamed to VocabParallelEmbedding; custom forwards that call
the root norm before lm_head must call _pre_lm_head_norm_and_gather (or add
maybe_gather_tp_input) instead.

Bug fix (Muse Spark): on main, Muse Spark's token embedding receives no
gradient under TP. The implicit boundary redistribution runs with
async_op=True (#5398) and returns an AsyncCollectiveTensor; it flows straight
into the compiled `muse_spark_rmsnorm` region, and torch.compile returns an
output with requires_grad=False, silently cutting the graph. The explicit
reduction here is synchronous like RowParallelLinear's, so the embedding trains
again. Verified with seed 42: at TP2+EP2 every step-1 parameter gradient is
bitwise identical to main except tok_embeddings.embedding.weight (None on main,
0.0590 here; 0.0564 at TP1). The async/compile interaction itself is an
upstream issue; this change only stops exercising it at these boundaries.

Numerics: bitwise-identical loss and grad_norm over 10 steps vs. main with
deterministic seed 42 on llama3 (TP2+SP, TP2 no-SP, TP2+CP2), qwen3 (TP2+SP),
deepseek_v3 with MTP (TP2+EP2, SP and no-SP), gpt_oss (TP2+EP2), and
deepseek_v4 (FSDP). Multimodal flavors (Qwen3.5 and Kimi K2.5 debug models,
which build a vision encoder) under TP2 with and without SP: full-model fwd+bwd
output and every parameter gradient bitwise identical to main (main itself
deterministic across two runs). Muse Spark differs only as described above.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests 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