Repository navigation
Add deterministic topk for MoE routing - #3600
Merged
Merged
Conversation
Route MoE expert selection through a TorchTitan custom op that enables PyTorch's deterministic topk implementation locally and restores the caller's deterministic-algorithm state afterward. This gives activation-checkpoint recompute the same stable top-k tie-breaking behavior without saving raw aten.topk outputs in selective activation checkpointing. The wrapper follows the deterministic_scatter_add pattern, includes fake tensor and autograd registrations, and relies on the PyTorch deterministic topk implementation when available. Test Plan: - python -m py_compile torchtitan/ops/topk.py torchtitan/models/common/moe.py torchtitan/distributed/activation_checkpoint.py tests/unit_tests/test_deterministic_ops.py - pytest tests/unit_tests/test_deterministic_ops.py -q - pytest tests/unit_tests/test_activation_checkpoint.py -q - pytest tests/unit_tests/test_compile_moe.py -q - pre-commit run --files torchtitan/ops/topk.py torchtitan/models/common/moe.py torchtitan/distributed/activation_checkpoint.py tests/unit_tests/test_deterministic_ops.py - pre-commit run --all-files stack-info: PR: #3600, branch: sanketpurandare/stack/20
sanketpurandare
force-pushed
the
sanketpurandare/stack/20
branch
from
June 10, 2026 01:42
c1033f9 to
bc36ecf
Compare
tianyu-l
reviewed
Jun 10, 2026
| torch.ops.aten.linear.default, | ||
| # topk can be non-deterministic; save to keep MoE expert assignments | ||
| # stable between forward and recompute. | ||
| torch.ops.aten.topk.default, |
Contributor
There was a problem hiding this comment.
just curious -- what makes "always saving topk" in SAC policy bad?
Contributor
Author
There was a problem hiding this comment.
Not bad but unnecessary.
| ) -> tuple[torch.Tensor, torch.Tensor]: | ||
| prev = torch.are_deterministic_algorithms_enabled() | ||
| prev_warn_only = torch.is_deterministic_algorithms_warn_only_enabled() | ||
| torch.use_deterministic_algorithms(True, warn_only=False) |
tianyu-l
approved these changes
Jun 11, 2026
sanketpurandare
added a commit
that referenced
this pull request
Jun 12, 2026
Reverts #3600 Upstream PyTorch PR: pytorch/pytorch#186653 was reverted due to failing internal tests. They have been using `topk` under the deterministic algorithms context incorrectly. So this change altered their numerics which was expected, but I will need to convince them that it is the right step forward. So reverting this for now, will reland when upstream op is landed.
saforem2
pushed a commit
to saforem2/torchtitan
that referenced
this pull request
Jun 12, 2026
Reverts pytorch#3600 Upstream PyTorch PR: pytorch/pytorch#186653 was reverted due to failing internal tests. They have been using `topk` under the deterministic algorithms context incorrectly. So this change altered their numerics which was expected, but I will need to convince them that it is the right step forward. So reverting this for now, will reland when upstream op is landed.
saforem2
added a commit
to saforem2/torchtitan
that referenced
this pull request
Jun 12, 2026
772dd1b Checkpointer refactor: exported names (CheckpointManager, ModelWrapper) preserved; ezpz imports cleanly. 1c02a5c revert of PR pytorch#3600 deterministic_topk: ezpz/moe re-imports TokenChoiceTopKRouter from common/moe.py, change flows through. Static smoke OK. Skipping dynamic smoke — last sync's agpt_2b_chunkedce 20-step bitwise IDENTICAL already covered checkpoint/optimizer code paths and the topk revert is a no-op for ezpz.
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.
Route MoE expert selection through a TorchTitan custom op that enables PyTorch's deterministic topk implementation locally and restores the caller's deterministic-algorithm state afterward. This gives activation-checkpoint recompute the same stable top-k tie-breaking behavior without saving raw aten.topk outputs in selective activation checkpointing.
The wrapper follows the deterministic_scatter_add pattern, includes fake tensor and autograd registrations, and relies on the PyTorch deterministic topk implementation when available.
Test Plan:
python -m py_compile torchtitan/ops/topk.py torchtitan/models/common/moe.py torchtitan/distributed/activation_checkpoint.py tests/unit_tests/test_deterministic_ops.py
pytest tests/unit_tests/test_deterministic_ops.py -q
pytest tests/unit_tests/test_activation_checkpoint.py -q
pytest tests/unit_tests/test_compile_moe.py -q
pre-commit run --files torchtitan/ops/topk.py torchtitan/models/common/moe.py torchtitan/distributed/activation_checkpoint.py tests/unit_tests/test_deterministic_ops.py
pre-commit run --all-files