-
Notifications
You must be signed in to change notification settings - Fork 34.1k
[WIP] Enable reproducibility for distributed trainings #16907
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
Changes from 3 commits
87ac401
df53811
041d20d
77e8308
2e40858
7ee6c68
43b3669
3c1e31a
f8f8926
1066995
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -46,7 +46,38 @@ | |||||
| import tensorflow as tf | ||||||
|
|
||||||
|
|
||||||
| def set_seed(seed: int): | ||||||
| def seed_worker(): | ||||||
| """ | ||||||
| Helper function to set worker seed during Dataloader initialization. | ||||||
| """ | ||||||
| worker_seed = torch.initial_seed() % 2**32 | ||||||
|
hasansalimkanmaz marked this conversation as resolved.
|
||||||
| set_seed(worker_seed) | ||||||
|
|
||||||
|
|
||||||
| def enable_determinism_for_distributed_training(): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea would be for this function to take
Suggested change
and then call (Also changing the name to be a bit shorter.) |
||||||
| """ | ||||||
| Helper function for reproducible behavior during distributed training. See | ||||||
| - https://pytorch.org/docs/stable/notes/randomness.html for pytorch | ||||||
| - https://www.tensorflow.org/api_docs/python/tf/config/experimental/enable_op_determinism for tensorflow | ||||||
| """ | ||||||
|
|
||||||
| if is_torch_available(): | ||||||
| # Enable PyTorch deterministic mode. This potentially requires either the environment | ||||||
| # variable 'CUDA_LAUNCH_BLOCKING' or 'CUBLAS_WORKSPACE_CONFIG' to be set, | ||||||
| # depending on the CUDA version, so we set them both here | ||||||
| os.environ["CUDA_LAUNCH_BLOCKING"] = "1" | ||||||
| os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8" | ||||||
| torch.use_deterministic_algorithms(True) | ||||||
|
|
||||||
| # Enable CUDNN deterministic mode | ||||||
| torch.backends.cudnn.deterministic = True | ||||||
| torch.backends.cudnn.benchmark = False | ||||||
|
|
||||||
| if is_tf_available(): | ||||||
| tf.config.experimental.enable_op_determinism() | ||||||
|
|
||||||
|
|
||||||
| def set_seed(seed: int, enable_determinism: bool = True): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I like |
||||||
| """ | ||||||
| Helper function for reproducible behavior to set the seed in `random`, `numpy`, `torch` and/or `tf` (if installed). | ||||||
|
|
||||||
|
|
@@ -61,6 +92,8 @@ def set_seed(seed: int): | |||||
| # ^^ safe to call this function even if cuda is not available | ||||||
| if is_tf_available(): | ||||||
| tf.random.set_seed(seed) | ||||||
| if enable_determinism: | ||||||
| enable_determinism_for_distributed_training() | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And so this part would disappear here, it would be the other way around. |
||||||
|
|
||||||
|
|
||||||
| class EvalPrediction: | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.