Skip to content

upgrade spmd_types to 0.2.5, remove SpmdLayout - #4265

Merged
pianpwk merged 18 commits into
mainfrom
gh/pianpwk/86/head
Aug 26, 2026
Merged

pianpwk merged 18 commits into
mainfrom
gh/pianpwk/86/head

Conversation

@pianpwk

@pianpwk pianpwk commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 21, 2026
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 21, 2026
Comment thread torchtitan/components/loss.py Outdated
Comment on lines +256 to +260
spmd.assert_type(loss, {"dp": spmd.P, "cp": spmd.P})
spmd.assert_type(
global_valid_tokens,
{"dp": spmd.R, "cp": spmd.R, "tp": spmd.I},
)

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.

sometimes you only do assert type when type checking is on -- what's the principle?

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.

ah, one thing is that we have single-CPU unit tests on some modules, and assert_type will look for the ambient mesh to reference named axes against. We don't have mesh setup for these tests, so "dp/cp/tp" don't exist for single CPU.

We could set it up (fake backend), but it feels a bit overkill and more machinery for generic module use

Comment thread torchtitan/distributed/parallel_dims.py Outdated
@dataclass(frozen=True, slots=True)
class SpmdLayout:
"""Temporary SPMD layout annotations keyed by logical mesh axis name.
SpmdLayout = spmd.SpmdType

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.

can we deprecate this indirection of typing and directly use SpmdType?

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.

sure, just trying to reduce blast radius of PR

Comment thread torchtitan/distributed/parallel_dims.py Outdated
)
result[axis_name] = spmd.S(dim)
return result
def layout_axes(layout: SpmdLayout) -> tuple[MeshAxisName, ...]:

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.

feels like should be in spmd_types repo?

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.

yes, probably just missed this one as a helper

[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 22, 2026
ghstack-source-id: 661bad2
Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk pianpwk changed the title Upgrade spmd_types to 0.2.4 Upgrade spmd_types to 0.2.4, remove SpmdLayout Aug 24, 2026
@pianpwk pianpwk changed the title Upgrade spmd_types to 0.2.4, remove SpmdLayout upgrade spmd_types to 0.2.4, remove SpmdLayout Aug 24, 2026
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 24, 2026
ghstack-source-id: 41122ee
Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 24, 2026
ghstack-source-id: 3bfeb31
Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
@pianpwk pianpwk changed the title upgrade spmd_types to 0.2.4, remove SpmdLayout upgrade spmd_types to 0.2.5, remove SpmdLayout Aug 24, 2026
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk
pianpwk marked this pull request as ready for review August 24, 2026 23:22
Comment thread torchtitan/distributed/parallel_dims.py Outdated
)
result[axis_name] = spmd.S(dim)
return result
def layout_axes(layout: SpmdType) -> tuple[MeshAxisName, ...]:

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.

  1. should be put in spmd_types.py
  2. should rename because "layout" is gone; maybe to spmd_types_axes or spmd_axes?

Comment thread torchtitan/distributed/spmd_types.py Resolved
Comment thread torchtitan/models/common/attention.py Outdated


@spmd.no_typecheck(out_types=spmd.PartitionSpec(("dp", "cp"), "tp", None))
def varlen_attn_fn(*args, **kwargs):

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.

nit: asymmetric naming, one with _fn, the other without

Can we import varlen_attn as _varlen_attn and name this one by varlen_attn?

@tianyu-l tianyu-l mentioned this pull request Aug 25, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Comment thread torchtitan/models/common/attention.py Outdated
window_size=self.window_size,
**varlen_kwargs,
)
run_varlen_attn = varlen_attn if out_transform is None else varlen_attn_with_lse

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.

nit

Suggested change
run_varlen_attn = varlen_attn if out_transform is None else varlen_attn_with_lse
varlen_attn_fn = varlen_attn if out_transform is None else varlen_attn_with_lse

submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 25, 2026
ghstack-source-id: 1929467
Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk
pianpwk changed the base branch from gh/pianpwk/86/base to main August 26, 2026 16:38
@pianpwk
pianpwk merged commit 8179a3a into main Aug 26, 2026
3 checks passed
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 26, 2026
ghstack-source-id: 6fdda7f
Pull Request resolved: pytorch/torchtitan#4265
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Aug 28, 2026
…helper

21 commits behind upstream/main. Merged into a scratch clone (../tt-sync82)
rather than the production one, because three optcmp arms are mid-chain with
7 links queued on afterany: each released link re-reads the working tree at
launch, so landing this now would have later links training on different code
than earlier ones and silently break comparability of the +10B measurement
in progress.

The merge is clean -- zero conflicts across 127 files -- and agpt is fine:
all three optcmp configs build, and SpmdLayout (removed by pytorch#4265) turns out
to appear in ezpz only in comments and as a local variable name.

ezpz/moe does not build. pytorch#4300 rewrote models/utils.py and removed
get_moe_model_nparams_and_flops, which moe/model.py imports at :30 and calls
at :344. It is a port rather than a rename: the replacement
get_nparams_and_active_nparams takes (model, *, modules_excluded...) and
returns (nparams, active_nparams), dropping flops, which are now composed
separately via quadratic_attention_flops_per_token. deepseek_v3's
get_deepseek_v3_nparams_and_flops is the reference pattern.

Landing plan is unchanged: finish the +10B run, then merge, then 2N smoke,
with pytorch#4116 (post-dataloading refactor) checked against BlendCorpus
specifically since that is where the token-unit fold bugs have lived.
tianyu-l pushed a commit that referenced this pull request Sep 2, 2026
Human: this is just cleaning up some deprecated stuff, moving it to the
new API

## Why

`@spmd.register_autograd_function` only does one meaningful thing: it
adds a class to `_TYPECHECK_AUTOGRAD_FUNCTIONS` so the checker will
route it to a legacy `typecheck_forward` staticmethod. At dispatch the
checker looks for a `spmd_typecheck` staticmethod on the class *first*,
via plain `getattr` -- no registry consulted -- and only falls back to
the registry set for classes that lack it. So for a `spmd_typecheck`
class the bare decorator is vestigial: it puts the class in a set nobody
reads for it.

`_LossParallelCrossEntropy` was the last thing in torchtitan still on
the legacy protocol. #4265 moved every other autograd function
(`AllGatherLinear`, `LinearReduceScatter`, `_FusedMLAQ`, `_FusedMLAKV`,
the GPT-OSS bias scaler, ...) to `spmd_typecheck` and dropped the
decorator with it; loss.py was missed. This finishes that migration, and
with it the last use of `register_autograd_function` in the repo.

## What

Same assertions, new hook shape. `typecheck_forward` had to re-dispatch
`.apply()` itself and then stamp the output by hand:

```python
result = _LossParallelCrossEntropy.apply(logits, labels, tp_group, global_vocab_size, reduction)
output_type = dict(spmd.get_local_type(logits))
output_type[tp_group] = spmd.I
spmd.assert_type(result, output_type)
```

`spmd_typecheck` is handed the result, so "logits' local type with the
TP axis overridden to `I`" is just `assert_local_type_like`, matching
how `torchtitan/distributed/linear.py` spells the same idea.

`global_vocab_size` and `reduction` are dropped from the hook signature
-- neither affects the SPMD type, and the hook is called with apply's
arguments by name, so accepting a subset is fine (`_FusedMLAQ` already
omits `q_nope_dim` this way).

## Test plan


`tests/unit_tests/cpu/test_loss.py::test_loss_parallel_cross_entropy_parity`
calls `.apply()` under `typecheck(strict_mode="strict")` and asserts
`get_axis_local_type(local_loss, tp_group) is spmd.I`, over TP-only and
DP+TP meshes with even/uneven vocab sharding and `IGNORE_INDEX` labels
-- so it exercises the new dispatch path directly. No numerics change:
this only touches the type-checking hook, not `forward`/`backward`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants