Repository navigation
[spmd] Remove implicit ShardingConfig redistribution - #5807
anijain2305 merged 1 commit into
Conversation
bcac78d to
6385e37
Compare
6385e37 to
d104998
Compare
d104998 to
0fc191e
Compare
0fc191e to
b697c7f
Compare
b697c7f to
64a4db7
Compare
64a4db7 to
52662bd
Compare
52662bd to
81299e5
Compare
81299e5 to
6d57a49
Compare
6d57a49 to
ffc73cc
Compare
ffc73cc to
37d6721
Compare
37d6721 to
fdca261
Compare
3a0237c to
cc43d8c
Compare
cc43d8c to
e5ebdcc
Compare
e5ebdcc to
6ecccd7
Compare
|
@claude review |
|
Claude finished @anijain2305's task in 2m 28s —— View job Approve (please fix 2 stale comments before merging): review |
There was a problem hiding this comment.
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
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.torchtitan/distributed/local_compile.py:41(_wait_async_collective_inputsdocstring) andtests/unit_tests/cpu/config/test_compile_config.py:106-- both still name "Module boundaries" as the source of un-awaitedAsyncCollectiveTensors. That producer is gone. Theasync_op=Truecallers left are the explicit CP collectives inmodels/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.
| """ | ||
|
|
||
| state_shardings: dict[str, SpmdType] = field(default_factory=dict) | ||
| in_src_shardings: dict[str, SpmdType] | None = None |
There was a problem hiding this comment.
| in_src_shardings: dict[str, SpmdType] | None = None | |
| input_shardings: dict[str, SpmdType] | None = None |
| 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 |
There was a problem hiding this comment.
| out_src_shardings: SpmdType | tuple[SpmdType, ...] | None = None | |
| output_shardings: SpmdType | tuple[SpmdType, ...] | None = None |
6ecccd7 to
a9b1693
Compare
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.
a9b1693 to
1cb2545
Compare
Summary (human-written)
Get rid of implicit redistribution. Yay!!
Why
ShardingConfigcould insert collectives implicitly: when a module'sin_dst_shardings/out_dst_shardingsdiffered from itsin_src_shardings/out_src_shardings, theModulewrapper redistributed the tensor at theboundary (
spmd_redistribute_per_axis, async). Those collectives were invisiblein model code, easy to add by accident, and ran with
async_op=True(#5915tracks a gradient bug in that path).
The earlier PRs in this stack moved every such collective into explicit
spmd.redistributecalls in module forwards (#5802 decoder embedding and finalnorm, #5803 multimodal encoders, #5805 Transformers backend, #5806 the
post-scatter restore). After them, every remaining
dstentry equals itssrccounterpart, 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).
What changed
Core
ShardingConfigdropsin_dst_shardingsandout_dst_shardings. It onlydeclares layouts: parameter/buffer placements and the activation types at
the boundary.
in_src_shardings/out_src_shardingsare renamedto
input_shardings/output_shardings(review), at every call site.Module._parallelizewrapsforwardwith 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_redistributionsand the asyncmodule-boundary collective path are deleted from
distributed/spmd_types.py.Configs
src == dstentries are removed from the core shardinghelpers (
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'shf_sharding.py.Docs
SHARDING.md, the Transformers backendadd_moe_modelskill and theinner-attention docstring described boundary redistribution; they now
describe the explicit collectives. (The MoE collectives were already
explicit in
MoE.forwardon 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
dstfields but assertdst == src: keeps a field whose onlyvalid value is a copy of another, and the type still suggests the config can
move data.
is exactly what the stack removes; out-of-tree code can call
spmd.redistributein its forward.User-facing changes
ShardingConfig(..., in_dst_shardings=..., out_dst_shardings=...)TypeErrorat construction; declare the layout ininput_shardings/output_shardingsand callspmd.redistributeinforwardfor any layout changeShardingConfig(in_src_shardings=..., out_src_shardings=...)ShardingConfig(input_shardings=..., output_shardings=...)async_op=True)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):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 modelforward + backward at TP2 with SP on and off: output and all gradients bitwise
identical to the previous commit.
Testing
are removed with them.
Known issues / follow-ups
[spmd] Remove implicit ShardingConfig redistribution #5807).
lm_headgather fromlm_headitself(TODO in
Decoder._pre_lm_head_norm_and_gather); build text flavors as aDecoderand dropkeep_vision_encoder.