Repository navigation
Drop register_autograd_function from _LossParallelCrossEntropy - #4415
Merged
Merged
Conversation
_LossParallelCrossEntropy was the last holdout still using the legacy typecheck_forward protocol, which is the only thing @register_autograd_function actually gates: the checker snoops for a spmd_typecheck staticmethod on the class first and never consults the registry for it, so the registry set exists purely to route typecheck_forward classes. Everything else in torchtitan moved to spmd_typecheck in #4265; this finishes the migration and removes the last use of the decorator. The assertions are unchanged. typecheck_forward had to re-dispatch .apply() itself and stamp the output by copying logits' local type and overriding the TP axis with I; spmd_typecheck is called with the result already in hand, so the same output type is spelled with assert_local_type_like, matching AllGatherLinear and friends in torchtitan/distributed/linear.py. global_vocab_size and reduction are dropped from the hook because they do not affect the SPMD type. Test plan: tests/unit_tests/cpu/test_loss.py test_loss_parallel_cross_entropy_parity exercises .apply() under typecheck(strict_mode="strict") and asserts the TP-axis output type is I, so it covers the new dispatch path directly.
ezyang
marked this pull request as ready for review
September 2, 2026 15:58
ezyang
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 2, 2026 15:58
pianpwk
approved these changes
Sep 2, 2026
tianyu-l
approved these changes
Sep 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human: this is just cleaning up some deprecated stuff, moving it to the new API
Why
@spmd.register_autograd_functiononly does one meaningful thing: it adds a class to_TYPECHECK_AUTOGRAD_FUNCTIONSso the checker will route it to a legacytypecheck_forwardstaticmethod. At dispatch the checker looks for aspmd_typecheckstaticmethod on the class first, via plaingetattr-- no registry consulted -- and only falls back to the registry set for classes that lack it. So for aspmd_typecheckclass the bare decorator is vestigial: it puts the class in a set nobody reads for it._LossParallelCrossEntropywas 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, ...) tospmd_typecheckand dropped the decorator with it; loss.py was missed. This finishes that migration, and with it the last use ofregister_autograd_functionin the repo.What
Same assertions, new hook shape.
typecheck_forwardhad to re-dispatch.apply()itself and then stamp the output by hand:spmd_typecheckis handed the result, so "logits' local type with the TP axis overridden toI" is justassert_local_type_like, matching howtorchtitan/distributed/linear.pyspells the same idea.global_vocab_sizeandreductionare 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 (_FusedMLAQalready omitsq_nope_dimthis way).Test plan
tests/unit_tests/cpu/test_loss.py::test_loss_parallel_cross_entropy_paritycalls.apply()undertypecheck(strict_mode="strict")and assertsget_axis_local_type(local_loss, tp_group) is spmd.I, over TP-only and DP+TP meshes with even/uneven vocab sharding andIGNORE_INDEXlabels -- so it exercises the new dispatch path directly. No numerics change: this only touches the type-checking hook, notforward/backward.