Skip to content

[spmd] Remove implicit ShardingConfig redistribution - #5807

Merged
anijain2305 merged 1 commit into
explicit-multimodal-residual-layoutfrom
remove-implicit-redistribution
Nov 13, 2026
Merged

anijain2305 merged 1 commit into
explicit-multimodal-residual-layoutfrom
remove-implicit-redistribution

Conversation

@anijain2305

@anijain2305 anijain2305 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Get rid of implicit redistribution. Yay!!

Why

ShardingConfig could insert collectives implicitly: when a module's
in_dst_shardings / out_dst_shardings differed from its in_src_shardings /
out_src_shardings, the Module wrapper redistributed the tensor at the
boundary (spmd_redistribute_per_axis, async). Those collectives were invisible
in model code, easy to add by accident, and ran with async_op=True (#5915
tracks a gradient bug in that path).

The earlier PRs in this stack moved every such collective into explicit
spmd.redistribute calls in module forwards (#5802 decoder embedding and final
norm, #5803 multimodal encoders, #5805 Transformers backend, #5806 the
post-scatter restore). After them, every remaining dst entry equals its src
counterpart, so the boundary redistribution never fires. This PR removes the
machinery, so a new config can't silently reintroduce an implicit collective.

Example (a module boundary).

before: ShardingConfig(in_src_shardings={"x": S(0)}, in_dst_shardings={"x": R})
        -> Module wrapper: spmd_redistribute_per_axis(S(0) -> R, async_op=True)
after:  ShardingConfig(input_shardings={"x": S(0)})        # asserted only
        -> forward: x = spmd.redistribute(x, tp_group, src=S(0), dst=R)

What changed

Core

  • ShardingConfig drops in_dst_shardings and out_dst_shardings. It only
    declares layouts: parameter/buffer placements and the activation types at
    the boundary.
  • With no dst side left, in_src_shardings / out_src_shardings are renamed
    to input_shardings / output_shardings (review), at every call site.
  • Module._parallelize wraps forward with boundary checks only
    (_assert_input_types / _assert_output_type, under SPMD type checking)
    and builds local-SPMD regions from input_shardings /
    output_shardings.
  • spmd_redistribute_per_axis, spmd_validate_redistributions and the async
    module-boundary collective path are deleted from
    distributed/spmd_types.py.

Configs

  • The redundant src == dst entries are removed from the core sharding
    helpers (decoder_sharding.py, vision_encoder_sharding.py), the Qwen3.5,
    Kimi K3, DeepSeek-V4, Muse Spark and Muse Glimmer sharding files, the Muse
    Spark CP-replicate transform, NVFP4Linear, and the Transformers backend's
    hf_sharding.py.

Docs

  • MoE SHARDING.md, the Transformers backend add_moe_model skill and the
    inner-attention docstring described boundary redistribution; they now
    describe the explicit collectives. (The MoE collectives were already
    explicit in MoE.forward on main; the doc was out of date.)

Mechanical, skimmable: the field rename across models, configs, tests and docs; tests drop asserts on the removed fields, and the
tests of the deleted helpers (test_parallelism_context.py) are removed.

Alternatives considered

  • Keep dst fields but assert dst == src: keeps a field whose only
    valid value is a copy of another, and the type still suggests the config can
    move data.
  • Keep the machinery as an opt-in: a second way to issue collectives
    is exactly what the stack removes; out-of-tree code can call
    spmd.redistribute in its forward.

User-facing changes

Before After
ShardingConfig(..., in_dst_shardings=..., out_dst_shardings=...) TypeError at construction; declare the layout in input_shardings / output_shardings and call spmd.redistribute in forward for any layout change
ShardingConfig(in_src_shardings=..., out_src_shardings=...) ShardingConfig(input_shardings=..., output_shardings=...)
Boundary collectives ran async (async_op=True) No boundary collectives; the explicit ones in model code are synchronous

Out-of-tree modules that relied on boundary redistribution must issue the
collective themselves; a config that still sets the removed fields fails
loudly instead of silently getting a collective.

Numerics

Seed 42, deterministic, 10 steps; loss and grad_norm bitwise identical to main
(scripts/loss_compare.py --assert-equal):

  • llama3: TP2+SP, TP2 without SP, TP2+CP2;
  • qwen3: TP2+SP;
  • deepseek_v3 with MTP: TP2+EP2, with and without SP;
  • gpt_oss: TP2+EP2;
  • deepseek_v4: FSDP;
  • Transformers backend: dense debug model TP2 (with SP; this backend always
    enables SP with TP), and MoE FSDP+TP+EP+CP (8 GPUs).

Qwen3.5 and Kimi K2.5 (kimi_k2_7, KimiK25Model; debug models with a vision encoder), full model
forward + backward at TP2 with SP on and off: output and all gradients bitwise
identical to the previous commit.

Testing

  • CPU unit tests for the touched modules pass; tests of the deleted helpers
    are removed with them.

Known issues / follow-ups

@anijain2305
anijain2305 added this pull request to stack #5808 November 4, 2026 06:59
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 4, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 4, 2026
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from bcac78d to 6385e37 Compare November 4, 2026 07:14
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 6385e37 to d104998 Compare November 6, 2026 22:44
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from d104998 to 0fc191e Compare November 6, 2026 23:05
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 0fc191e to b697c7f Compare November 6, 2026 23:10
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from b697c7f to 64a4db7 Compare November 6, 2026 23:35
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 64a4db7 to 52662bd Compare November 6, 2026 23:49
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 52662bd to 81299e5 Compare November 7, 2026 00:06
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 81299e5 to 6d57a49 Compare November 11, 2026 20:21
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 6d57a49 to ffc73cc Compare November 11, 2026 20:38
@anijain2305
anijain2305 removed this pull request from stack #5808 November 11, 2026 20:40
@anijain2305
anijain2305 added this pull request to stack #6036 November 11, 2026 20:40
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from ffc73cc to 37d6721 Compare November 12, 2026 00:03
@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 37d6721 to fdca261 Compare November 12, 2026 00:12
@anijain2305

Copy link
Copy Markdown
Contributor Author

@claude review

@anijain2305
anijain2305 marked this pull request as ready for review November 13, 2026 00:14
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 13, 2026
@claude

claude Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @anijain2305's task in 2m 28s —— View job


Approve (please fix 2 stale comments before merging): review

@github-actions
github-actions Bot requested a review from pianpwk November 13, 2026 00:14

@claude claude Bot 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.

Verdict: Approve -- please address the items below before merging -- once the earlier PRs in the stack land, every dst entry equals its src, so removing the boundary-redistribution machinery is dead-code deletion with no numerics change (-531 LoC). A maintainer still needs to review, since this touches models/base/.

What this PR does. ShardingConfig loses in_dst_shardings / out_dst_shardings. Module._parallelize now wraps forward with type assertions only, and spmd_redistribute_per_axis / spmd_validate_redistributions are deleted. I checked every *_dst_shardings use in the parent commit (a9917e4): each one is the same object or value as its src counterpart. That includes muse_spark/flavors.py:523, where routed_experts_sharding_config() already has out_src = dense_sequence_parallel_placement(). For identical types the old spmd_redistribute_per_axis skipped every axis and returned x unchanged. So the removed path never issued a collective, and loss and grad_norm can't change. ShardingConfig is slots=True, so a leftover attribute assignment also fails with AttributeError instead of being silently ignored.

Example.

before: head_norm_sharding_config()
          in_src={"input": head_layout}, in_dst={"input": head_layout}
          out_src=head_layout,           out_dst=head_layout
        -> wrapper: assert_type, then spmd_redistribute_per_axis(src == dst) -> returns x
after:  in_src={"input": head_layout}, out_src=head_layout
        -> wrapper: assert_type only (and only under spmd type checking)

Must fix

  1. torchtitan/experiments/transformers_modeling_backend/parallelize.py:106-108 -- this comment still says "the flex kernel's local SPMD boundary redistributes k/v from seq-sharded to CP-Replicate (all-gather)". After this PR a boundary can't redistribute anything. The comment also contradicts _attach_flex_kernel's docstring (hf_sharding.py:307-313), which says the gather is an explicit funcol call in _wrap_flex_kernel_cp. The PR already edits the sibling comment at :148. Reword this one to point at _wrap_flex_kernel_cp.
  2. torchtitan/distributed/local_compile.py:41 (_wait_async_collective_inputs docstring) and tests/unit_tests/cpu/config/test_compile_config.py:106 -- both still name "Module boundaries" as the source of un-awaited AsyncCollectiveTensors. That producer is gone. The async_op=True callers left are the explicit CP collectives in models/common/attention/cp_mla.py:115,129,153. Name those (or drop the example) so nobody later deletes the workaround thinking it has no consumer.

Questions

  • The Numerics section mentions "Kimi K2.5". The in-repo model is kimi_k2_7. Is that what was tested?

Not verified: the GPU loss comparisons in the description. CPU reasoning only: removed dst == src at every site in the parent commit, so no code path issued a collective.

@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.

please fix naming!!

Comment thread torchtitan/models/base/sharding.py Outdated
"""

state_shardings: dict[str, SpmdType] = field(default_factory=dict)
in_src_shardings: dict[str, SpmdType] | None = None

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.

Suggested change
in_src_shardings: dict[str, SpmdType] | None = None
input_shardings: dict[str, SpmdType] | None = None

Comment thread torchtitan/models/base/sharding.py Outdated
state_shardings: dict[str, SpmdType] = field(default_factory=dict)
in_src_shardings: dict[str, SpmdType] | None = None
in_dst_shardings: dict[str, SpmdType] | None = None
out_src_shardings: SpmdType | tuple[SpmdType, ...] | None = None

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.

Suggested change
out_src_shardings: SpmdType | tuple[SpmdType, ...] | None = None
output_shardings: SpmdType | tuple[SpmdType, ...] | None = None

@anijain2305
anijain2305 force-pushed the remove-implicit-redistribution branch from 6ecccd7 to a9b1693 Compare November 13, 2026 03:03
The earlier PRs in this stack moved every collective that ShardingConfig's
src -> dst pairs used to insert into explicit `spmd.redistribute` calls in
module forwards. After them, every remaining `in_dst_shardings` /
`out_dst_shardings` entry equals its `src` counterpart, so the boundary
redistribution never issues a collective. This removes the machinery so a
new config can't silently reintroduce an implicit collective:

- `ShardingConfig` drops `in_dst_shardings` and `out_dst_shardings`. It now
  only declares layouts; any layout change is explicit code. With no dst side
  left, `in_src_shardings` / `out_src_shardings` are renamed to
  `input_shardings` / `output_shardings`.
- `Module._parallelize` only asserts boundary types (`_assert_input_types`,
  `_assert_output_type`) and builds local regions from `input_shardings` /
  `output_shardings`.
- `spmd_redistribute_per_axis` and `spmd_validate_redistributions` are deleted,
  along with the async module-boundary collective path.
- The redundant src == dst entries are removed from core sharding helpers,
  the muse_spark CP-replicate transform, NVFP4Linear and the HF backend.
- Docs that described boundary redistribution (MoE SHARDING.md, the HF
  add_moe_model skill, the inner-attention docstring) now describe the
  explicit collectives.

Tests: asserts on the removed fields are dropped; the tests of the deleted
helpers are removed.

BC: user configs that set `in_dst_shardings` / `out_dst_shardings` now fail
with a TypeError at construction instead of silently getting a collective;
configs that set `in_src_shardings` / `out_src_shardings` must use
`input_shardings` / `output_shardings`.
Out-of-tree modules that relied on boundary redistribution must call
`spmd.redistribute` themselves.

Numerics (seed 42, deterministic, 10 steps, loss and grad_norm bitwise
identical to main via scripts/loss_compare.py --assert-equal):
llama3 TP2+SP, TP2 no-SP, TP2+CP2; qwen3 TP2+SP; deepseek_v3 MTP TP2+EP2
with and without SP; gpt_oss TP2+EP2; deepseek_v4 FSDP; HF backend dense
TP2 (with SP; the backend always enables SP with TP); HF backend MoE
FSDP+TP+EP+CP (8 GPUs).
qwen3_5 and kimi_k2_7 full model fwd+bwd under TP2 (SP on/off): output and
all grads bitwise identical to the previous commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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