Repository navigation
Conversation
Since torch 2.13, torch.random.fork_rng defaults device_type to the current accelerator instead of CUDA. deterministic_rng passes it the CUDA device list, so wherever the current accelerator is not CUDA (MPS on Apple Silicon) that list is snapshotted and restored through the wrong backend. On real hardware the list is empty there, but it makes test_device_generators_are_seeded_inside_and_restored_after fail on macOS. Pass device_type="cuda", and have the test fixture report MPS as the current accelerator so the case is covered on every runner.
This branch has not been deployed
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.
Description
deterministic_rng(added totransformer_lens/benchmarks/utils.pyin #1934) forks the CUDA generators withSince torch 2.13,
fork_rngno longer defaultsdevice_typeto"cuda": it usestorch.accelerator.current_accelerator()and falls back to CUDA only when there is none. Where the current accelerator is not CUDA (MPS on Apple Silicon), the CUDA device list is snapshotted and restored throughtorch.mpsinstead, so the CUDA generators are not restored after the block.On real hardware this is harmless today, because a Mac has no CUDA devices and the list is empty. It does make
tests/unit/benchmarks/test_device_generators_are_seeded_inside_and_restored_afterfail on macOS with torch >= 2.13 (thedevice_generatorsfixture fakes both CUDA and MPS):CI does not hit this: the unit tests run on Linux runners with no accelerator (so
fork_rngfalls back to CUDA), and the macOS job uses the locked torch 2.11.Changes:
device_type="cuda"tofork_rng, so the CUDA fork does not depend on torch's default (the argument has existed since well before torch 2.6);device_generatorsfixture also report MPS as the current accelerator, as on a Mac, so the test covers this on every runner.34c5d77fWith torch 2.11
fork_rngdoes not consult the accelerator, so the fixture change is a no-op there.Type of change
Checklist:
No new test: the existing
test_device_generators_are_seeded_inside_and_restored_afteralready covers this once its fixture reports MPS as the current accelerator.Local runs:
tests/unit -m "not slow"on macOS with torch 2.14.1 and transformers 5.20.2: 7651 passed, 0 failed;tests/unit/benchmarkson both torch 2.14.1 and 2.11.0 (101 passed each), plus black, isort, pycln andmypy ..