Repository navigation
upgrade spmd_types to 0.2.5, remove SpmdLayout - #4265
Conversation
[ghstack-poisoned]
ghstack-source-id: 59e4cf3 Pull-Request: pytorch/torchtitan#4265
| 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}, | ||
| ) |
There was a problem hiding this comment.
sometimes you only do assert type when type checking is on -- what's the principle?
There was a problem hiding this comment.
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
| @dataclass(frozen=True, slots=True) | ||
| class SpmdLayout: | ||
| """Temporary SPMD layout annotations keyed by logical mesh axis name. | ||
| SpmdLayout = spmd.SpmdType |
There was a problem hiding this comment.
can we deprecate this indirection of typing and directly use SpmdType?
There was a problem hiding this comment.
sure, just trying to reduce blast radius of PR
| ) | ||
| result[axis_name] = spmd.S(dim) | ||
| return result | ||
| def layout_axes(layout: SpmdLayout) -> tuple[MeshAxisName, ...]: |
There was a problem hiding this comment.
feels like should be in spmd_types repo?
There was a problem hiding this comment.
yes, probably just missed this one as a helper
[ghstack-poisoned]
ghstack-source-id: 661bad2 Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
ghstack-source-id: 41122ee Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
ghstack-source-id: 3bfeb31 Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
| ) | ||
| result[axis_name] = spmd.S(dim) | ||
| return result | ||
| def layout_axes(layout: SpmdType) -> tuple[MeshAxisName, ...]: |
There was a problem hiding this comment.
- should be put in spmd_types.py
- should rename because "layout" is gone; maybe to
spmd_types_axesorspmd_axes?
|
|
||
|
|
||
| @spmd.no_typecheck(out_types=spmd.PartitionSpec(("dp", "cp"), "tp", None)) | ||
| def varlen_attn_fn(*args, **kwargs): |
There was a problem hiding this comment.
nit: asymmetric naming, one with _fn, the other without
Can we import varlen_attn as _varlen_attn and name this one by varlen_attn?
[ghstack-poisoned]
[ghstack-poisoned]
| window_size=self.window_size, | ||
| **varlen_kwargs, | ||
| ) | ||
| run_varlen_attn = varlen_attn if out_transform is None else varlen_attn_with_lse |
There was a problem hiding this comment.
nit
| 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 |
ghstack-source-id: 1929467 Pull Request resolved: pytorch/torchtitan#4265
[ghstack-poisoned]
ghstack-source-id: 6fdda7f Pull Request resolved: pytorch/torchtitan#4265
…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.
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`.
Stack from ghstack (oldest at bottom):
spmd_typesbackend #4325