Skip to content

spmd_types docs - #3853

Closed
pianpwk wants to merge 1 commit into
mainfrom
pianpwk/spmd_titan_md
Closed

pianpwk wants to merge 1 commit into
mainfrom
pianpwk/spmd_titan_md

Conversation

@pianpwk

@pianpwk pianpwk commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 1, 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.

put under distributed/ or distributed/spmd_types/

in general I'd like to remove this centralized doc folder to keep related things sitting together.

@pianpwk
pianpwk force-pushed the pianpwk/spmd_titan_md branch 2 times, most recently from 5ec3323 to 5bc70f0 Compare July 1, 2026 22:42
@pianpwk
pianpwk force-pushed the pianpwk/spmd_titan_md branch 3 times, most recently from 175801d to f2e74ab Compare July 1, 2026 22:44
activation or parameter may be sharded, replicated, pending an all-reduce over a mesh dimension,
or in some other weird form. DTensor is the familiar PyTorch-native example of global SPMD:
APIs like `redistribute` express placement-based collectives, while DTensor's sharding propagator
and redistribution engine decide which operations comply with global SPMD.

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.

not sure how much to talk about DTensor, or why it's bad

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.

I think expanding a little on DTensor's drawbacks will help motivate the next section introducing spmd_types

@pianpwk
pianpwk force-pushed the pianpwk/spmd_titan_md branch from f2e74ab to 74c4176 Compare July 1, 2026 22:48
custom autograd functions.

For more background, see the [`spmd_types` repository](https://github.com/meta-pytorch/spmd_types)
and its reference docs.

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.

not sure how much more to talk about this, or link to other stuff

Comment thread torchtitan/distributed/spmd_types.md Outdated
invoking the ShardingConfig, and wire AC, compile, FSDP, and other features, using existing implementations as reference.
4. Define a trainer/dataloader-side function that annotates SPMD types for model inputs, if not covered by existing trainers.
Note that any input shardings or splits should be implemented on the dataloader side.
5. Wire the parallelization function into `parallelize_fn`, in model registries.

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.

is this too detailed, maybe it's likely to change

Comment thread torchtitan/distributed/spmd_types.md Outdated
`default` and `full_dtensor` backends use DTensor for parallelism, and the same declarative sharding
config system works for them; TorchTitan translates `spmd_types` placements into DTensor ones.

For a new model or module, the preferred flow is:

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.

actually don't have experience contributing a new model from scratch, so this might differ

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.

Comment thread torchtitan/distributed/spmd_types.md Outdated
During development, the `--debug.spmd_typechecking` should invoke the global SPMD typechecker over trainer FWD steps, catching
any incorrect or unannotated distributed computation. Real usage should avoid this flag, as it adds significant overhead.

The expectation when a new model, module, or override is contributed is that ......

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.

Some open questions I wasn't sure

  • If someone's adding a new model, it probably should typecheck under 1D / all combinations of parallelisms?
  • Same thing for one-off fixes, or adding new module?
  • For overrides: currently we typecheck helion rope & fused swiglu, but maybe we could refer back to the original impl it's overriding, maybe via register_decomposition or similar?
  • Bitwise guarantees: for backend migration bitwise was relatively easy to define (minus DTensor doing weird BWD stuff. Not sure what this is for a totally new model

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.

Pointers to relevant examples for the different escape hatches, disabling type checking, reinterpeting meshes, etc. will be helpful apart from the pseudocode examples.

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.

If type check fails, what are next remedial steps? Illustrating with a small example could be useful for agents to fix errors.

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.

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.

For overrides: currently we typecheck helion rope & fused swiglu, but maybe we could refer back to the original impl it's overriding, maybe via register_decomposition or similar?

If it's module boundary sharding, then the override author should include proper sharding_config if it changes, like in https://github.com/pytorch/torchtitan/blob/main/torchtitan/overrides/fused_swiglu.py#L498-L502

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.

If someone's adding a new model, it probably should typecheck under 1D / all combinations of parallelisms?

On all parallelisms that are supported, which is not different from DTensor-based model

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.

Same thing for one-off fixes, or adding new module?

Don't have to worry in this .md

- Custom ops/overrides/external library calls
- Wrap with `spmd.local_map()`,
or `spmd.no_typecheck` in some cases, and correctly assert input & output
SPMD types.

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.

better system for custom ops later

Comment thread torchtitan/distributed/spmd_types.md Outdated

## Compatibility expectations

...... typechecks? bitwise? compared to?

@pianpwk pianpwk Jul 1, 2026 •

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.

or any other sections, anything to make this more agent-friendly, in case of adding new models. don't think it's much of the latter right now.

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.

maybe mention current incompatibilities, including PP / flex+sac


The `with set_current_spmd_mesh(...)` context pushes a new DeviceMesh onto TorchTitan's
SPMD mesh stack, and updates the typechecker's active mesh context.
`spmd.reinterpret_mesh(tensor, type_or_mesh)` switches tensors onto the new current mesh,

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.

not clear why spmd.reinterpret_mesh is also required, and with set_current_spmd_mesh alone is not sufficient

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.

I asked a similar question in google doc

Comment thread torchtitan/distributed/spmd_types.md Outdated
APIs like `redistribute` express placement-based collectives, while DTensor's sharding propagator
and redistribution engine decide which operations comply with global SPMD.

`spmd_types` combines this placement-based programming model with added benefits:

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.

I wouldn't characterize this as added benefits. The old DTensor style guaranteed you could write single device code at the cost of implicit redistributes. Global spmd types checks you have written single device code but you have to write all the redistributes. Which is better is a value judgment.

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.

(repeating myself) having an illustration for the (potentially confusing) norm region (FSDP involved) would be helpful

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.

not sure if it should be SPMD_TYPES.md according to github convention.

Comment thread torchtitan/distributed/spmd_types.md Outdated
`default` and `full_dtensor` backends use DTensor for parallelism, and the same declarative sharding
config system works for them; TorchTitan translates `spmd_types` placements into DTensor ones.

For a new model or module, the preferred flow is:

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.

Comment thread torchtitan/distributed/spmd_types.md Outdated
During development, the `--debug.spmd_typechecking` should invoke the global SPMD typechecker over trainer FWD steps, catching
any incorrect or unannotated distributed computation. Real usage should avoid this flag, as it adds significant overhead.

The expectation when a new model, module, or override is contributed is that ......

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.

Comment thread torchtitan/distributed/spmd_types.md Outdated
During development, the `--debug.spmd_typechecking` should invoke the global SPMD typechecker over trainer FWD steps, catching
any incorrect or unannotated distributed computation. Real usage should avoid this flag, as it adds significant overhead.

The expectation when a new model, module, or override is contributed is that ......

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.

For overrides: currently we typecheck helion rope & fused swiglu, but maybe we could refer back to the original impl it's overriding, maybe via register_decomposition or similar?

If it's module boundary sharding, then the override author should include proper sharding_config if it changes, like in https://github.com/pytorch/torchtitan/blob/main/torchtitan/overrides/fused_swiglu.py#L498-L502

Comment thread torchtitan/distributed/spmd_types.md Outdated
During development, the `--debug.spmd_typechecking` should invoke the global SPMD typechecker over trainer FWD steps, catching
any incorrect or unannotated distributed computation. Real usage should avoid this flag, as it adds significant overhead.

The expectation when a new model, module, or override is contributed is that ......

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.

If someone's adding a new model, it probably should typecheck under 1D / all combinations of parallelisms?

On all parallelisms that are supported, which is not different from DTensor-based model

Comment thread torchtitan/distributed/spmd_types.md Outdated
During development, the `--debug.spmd_typechecking` should invoke the global SPMD typechecker over trainer FWD steps, catching
any incorrect or unannotated distributed computation. Real usage should avoid this flag, as it adds significant overhead.

The expectation when a new model, module, or override is contributed is that ......

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.

Same thing for one-off fixes, or adding new module?

Don't have to worry in this .md


The `with set_current_spmd_mesh(...)` context pushes a new DeviceMesh onto TorchTitan's
SPMD mesh stack, and updates the typechecker's active mesh context.
`spmd.reinterpret_mesh(tensor, type_or_mesh)` switches tensors onto the new current mesh,

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.

I asked a similar question in google doc

Comment thread torchtitan/distributed/spmd_types.md Outdated

## Compatibility expectations

...... typechecks? bitwise? compared to?

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.

maybe mention current incompatibilities, including PP / flex+sac

## Mesh transitions

MoE models in TorchTitan operate on 2 different meshes: the dense `[dp, cp, tp]` mesh
for the main transformer backbone, and the sparse `[dp_replicate, efsdp, ep]` mesh

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.

wait, this should be [edp, ep] to be consistent

@pianpwk pianpwk Jul 7, 2026 •

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.

hm good point.. I guess we never encountered issues because it's local, and spmd.V on both axes? Never had to deal with shard ordering.

also titan not having a efsdp flag means we coincidentally hit when ep * dp_replicate < NGPUs.

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.

also titan not having a efsdp flag means we coincidentally hit when ep * dp_replicate < NGPUs.

sorry I didn't understand "titan not having a efsdp flag" and "means we coincidentally hit (what?)"

Anyway we should resolve this and make it symmetric to the dense mesh.

@pianpwk
pianpwk force-pushed the pianpwk/spmd_titan_md branch from 75f687c to 379cc8a Compare July 31, 2026 16:19
@pianpwk
pianpwk marked this pull request as ready for review July 31, 2026 16:20
@pianpwk
pianpwk requested review from fegin, wconstab and wwwjn as code owners July 31, 2026 16:20
@pianpwk
pianpwk requested a review from tianyu-l July 31, 2026 16:20
@tianyu-l tianyu-l closed this Oct 7, 2026
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.

4 participants