Skip to content

Upgrade spmd_types to main - #5131

Merged
anijain2305 merged 2 commits into
mainfrom
gh/anijain2305/62/head
Oct 7, 2026
Merged

anijain2305 merged 2 commits into
mainfrom
gh/anijain2305/62/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):


  • Pin spmd_types to a main commit instead of the 0.2.5 release. It includes
    the fixes torch_remat activation checkpointing needs under tensor
    parallelism: metadata queries such as stride() and storage_offset() are
    no longer type checked, so saving a Partial region works, and
    assert_type_like / assert_local_type_like overrides keyed by a process
    group replace the source's entry for that axis instead of conflicting.
  • _AuxLossInjection.spmd_typecheck types its output with rules.output
    and declares aux_loss, which is only saved for backward, with
    rules.ignore, satisfying the new check that a hook accounts for every
    tensor argument.
  • spmd_types now ships type stubs: narrow a just-registered parameter or
    buffer before assert_type, and drop a no_typecheck suppression Pyrefly
    no longer needs.

[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 7, 2026
@pytorch-bot pytorch-bot Bot added ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 7, 2026
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 7, 2026
* Pin spmd_types to a main commit instead of the 0.2.5 release. It includes
  the fixes torch_remat activation checkpointing needs under tensor
  parallelism: metadata queries such as `stride()` and `storage_offset()` are
  no longer type checked, so saving a Partial region works, and
  `assert_type_like` / `assert_local_type_like` overrides keyed by a process
  group replace the source's entry for that axis instead of conflicting.
* `_AuxLossInjection.spmd_typecheck` types its output with `rules.output`
  and declares `aux_loss`, which is only saved for backward, with
  `rules.ignore`, satisfying the new check that a hook accounts for every
  tensor argument.
* spmd_types now ships type stubs: narrow a just-registered parameter or
  buffer before `assert_type`, and drop a `no_typecheck` suppression Pyrefly
  no longer needs.


ghstack-source-id: 02fb3ee
Pull-Request: pytorch/torchtitan#5131
@anijain2305
anijain2305 requested a review from pianpwk October 7, 2026 17:23
einops
pillow
spmd_types==0.2.5
spmd_types @ git+https://github.com/meta-pytorch/spmd_types.git@8b8a56e79b117cfac3ed289fa209f8358558d482

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 was worried you'd have to add rules.ignore in a bunch of places, but if CI is fine should be fine...

@pianpwk

pianpwk commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@claude do we not have to do that (use rules.ignore), do all the custom autograd functions already make use of all inputs

@claude

claude Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @pianpwk's task in 1m 56s —— View job


Answer

No. _AuxLossInjection is the only hook in the repo that needed a change. Every other spmd_typecheck hook already calls spmd.assert_type on each of its tensor inputs. That makes sense, since those hooks exist to state input layouts. _AuxLossInjection was different: its old hook didn't even take an aux_loss keyword, so nothing ever checked it. That's what the new rule caught.

I checked every spmd_typecheck in the repo (grep spmd_typecheck):

Hook Tensor inputs to forward Already covered in the hook?
_LossParallelCrossEntropy (torchtitan/components/loss.py:79) logits, labels Yes, both get assert_type
_VocabParallelEntropy (loss.py:221) logits Yes, assert_type
AsyncAllGatherLinear (torchtitan/models/common/async_linear.py:76) x_shard_m, w_shard_n, bias_shard_n (optional) Yes, all three (bias only when it is not None)
AsyncLinearReduceScatter (async_linear.py:175) x_shard_k, w_shard_k, bias (optional) Yes, all three (bias only when it is not None)
_FusedMLAQ (torchtitan_recipes/overrides/fused_mla.py:783) q, rope_cache_real, positions Yes, all three
_FusedMLAKV (fused_mla.py:841) kv, k_pe, rope_cache_real, positions Yes, all four
_AuxLossInjection (torchtitan/models/common/aux_loss.py:68) carrier, aux_loss No, before this PR: only result was checked against carrier, and aux_loss was never passed in. This PR fixes it with rules.ignore(aux_loss) and rules.output(result, carrier).

None of these hooks takes the non-tensor forward arguments (tp_group/group, group_name, global_vocab_size, reduction, q_nope_dim). As far as I can tell the new rule is about tensor arguments only, so leaving those out is fine.

"Using all inputs" in forward isn't the issue: aux_loss is used, since it's saved for backward. What matters is whether the hook states a type for each tensor input. _AuxLossInjection passes carrier straight through and only keeps aux_loss for its gradient, so it has no real input layout to state. rules.ignore is the right tool there. The other hooks are type checks of their inputs by design, so they already pass.

Caveats:

  • I couldn't open the spmd_types source at the pinned commit; running Python here needs approval. I'm assuming an assert_type call counts as accounting for an argument. That fits the PR description and the green CI, but I haven't read the checker code to confirm it.
  • The loss hooks have strict-mode typecheck tests in tests/unit_tests/cpu/test_loss.py. I didn't find a typecheck-enabled unit test for AsyncAllGatherLinear/AsyncLinearReduceScatter or the fused MLA hooks. They look fine from the code alone, but if those paths only run with typechecking off in CI, a mistake there wouldn't show up.

@anijain2305
anijain2305 changed the base branch from gh/anijain2305/62/base to main October 7, 2026 18:36
@anijain2305
anijain2305 merged commit dd4d483 into main Oct 7, 2026
anijain2305 added a commit that referenced this pull request Oct 7, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #5102
* #5101
* __->__ #4893
* #5131

----

Needs https://www.internalfb.com/diff/D123762808

This moves SelectiveAC to use torch remat. In future, we want to get rid
of SelectiveAC as well and just have one RegionAC (or a better name)
with possibly some good defaults for models. Currently, we keep the
`SelectiveAC` name because its used at many places and there is some
future work to remove it entirely.


* SelectiveAC is now a fixed torch_remat policy: it saves every
model-declared
region except the routed-expert `w13`/`w2` grouped matmuls, and
recomputes
everything else. It shares the `_RematAC` base with RegionAC, which
stays
  the way to choose a different policy; SelectiveAC can no longer be
  subclassed.
* The operator-level save policy and `force_recompute_mm_shapes_by_fqns`
are
removed. The graph trainer keeps its own copy of the operator save set.
* `configure_remat_regions` gains `save_all_except`, which SelectiveAC
uses: it
  suits starting from no AC and recomputing only a few regions.
* FullAC is unchanged and still uses PyTorch checkpointing.
* The SAC + FlexInnerAttention restriction under SPMD typechecking is
removed.


<img width="1933" height="833" alt="image"
src="https://github.com/user-attachments/assets/5937fbba-5e72-45db-acad-e6602e8b2340"
/>

<img width="1939" height="844" alt="image"
src="https://github.com/user-attachments/assets/f8932afb-5f9c-4fc9-8c95-d0046af341f8"
/>
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 7, 2026
* Pin spmd_types to a main commit instead of the 0.2.5 release. It includes
  the fixes torch_remat activation checkpointing needs under tensor
  parallelism: metadata queries such as `stride()` and `storage_offset()` are
  no longer type checked, so saving a Partial region works, and
  `assert_type_like` / `assert_local_type_like` overrides keyed by a process
  group replace the source's entry for that axis instead of conflicting.
* `_AuxLossInjection.spmd_typecheck` types its output with `rules.output`
  and declares `aux_loss`, which is only saved for backward, with
  `rules.ignore`, satisfying the new check that a hook accounts for every
  tensor argument.
* spmd_types now ships type stubs: narrow a just-registered parameter or
  buffer before `assert_type`, and drop a `no_typecheck` suppression Pyrefly
  no longer needs.


ghstack-source-id: 02fb3ee
Pull-Request: pytorch/torchtitan#5131
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 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