When torch.use_deterministic_algorithms(True) is enabled, make topk resolve tied values with stable index tie-breaking.
CPU topk now compares by value and then index in deterministic mode. CUDA keeps the existing top-k selection path and uses stable sorting for the selected top-k outputs so threshold ties preserve input-index order without falling back to a full sort on NVIDIA. ROCm uses stable sort in deterministic mode because its current top-k gather reserves tie slots through atomics.
This gives deterministic mode a topk behavior analogous to scatter_add: fast default kernels remain available, while deterministic mode uses stable algorithms when requested.
Test Plan:
Rebuilt PyTorch from source with CUDA enabled.
python -m py_compile test/test_sort_and_select.py torch/init.py torch/_torch_docs.py
python test/test_sort_and_select.py -k topk_deterministic
python test/test_sort_and_select.py -k topk
git diff --check HEAD^
lintrunner --config=.lintrunner.toml --skip=PYREFLY aten/src/ATen/native/TopKImpl.h aten/src/ATen/native/cuda/TensorTopK.cpp test/test_sort_and_select.py torch/init.py torch/_torch_docs.py
Pull Request resolved: #186653
Approved by: https://github.com/drisspg