Add deterministic topk for MoE routing#3600
Open
sanketpurandare wants to merge 1 commit into
Open
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
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) |
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