Skip to content

3546 Enhance set_determinism for global flags #3547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions monai/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ def set_determinism(
for func in additional_settings:
func(seed)

if torch.backends.flags_frozen():
warnings.warn("PyTorch global flag support of backends is disabled, enable it to set global `cudnn` flags.")
torch.backends.__allow_nonbracketed_mutation_flag = True

if seed is not None:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
Expand Down
2 changes: 2 additions & 0 deletions tests/test_set_determinism.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def test_values(self):
self.assertEqual(seed, get_seed())
a = np.random.randint(seed)
b = torch.randint(seed, (1,))
# tset when global flag support is disabled
torch.backends.disable_global_flags()
set_determinism(seed=seed)
c = np.random.randint(seed)
d = torch.randint(seed, (1,))
Expand Down