Skip to content

Drop register_autograd_function from _LossParallelCrossEntropy - #4415

Merged
tianyu-l merged 1 commit into
mainfrom
remove-register-autograd-function
Sep 2, 2026
Merged

tianyu-l merged 1 commit into
mainfrom
remove-register-autograd-function

Conversation

@ezyang

@ezyang ezyang commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

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:

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.

_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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 2, 2026
@ezyang
ezyang marked this pull request as ready for review September 2, 2026 15:58
@ezyang
ezyang requested a review from pianpwk September 2, 2026 15:59
@tianyu-l
tianyu-l merged commit 2438356 into main Sep 2, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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