Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/transformers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
get_last_checkpoint,
has_length,
number_of_arguments,
seed_worker,
set_seed,
speed_metrics,
)
Expand Down Expand Up @@ -704,6 +705,7 @@ def get_train_dataloader(self) -> DataLoader:
drop_last=self.args.dataloader_drop_last,
num_workers=self.args.dataloader_num_workers,
pin_memory=self.args.dataloader_pin_memory,
worker_init_fn=seed_worker,
Comment thread
hasansalimkanmaz marked this conversation as resolved.
)

def _get_eval_sampler(self, eval_dataset: Dataset) -> Optional[torch.utils.data.Sampler]:
Expand Down
35 changes: 34 additions & 1 deletion src/transformers/trainer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
hasansalimkanmaz marked this conversation as resolved.
set_seed(worker_seed)


def enable_determinism_for_distributed_training():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea would be for this function to take seed here.

Suggested change
def enable_determinism_for_distributed_training():
def enable_full_determinism(seed: int):

and then call set_seed inside (instead of set_seed calling this function).

(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):

@sgugger sgugger Apr 25, 2022

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def set_seed(seed: int, enable_determinism: bool = True):
def set_seed(seed: int, full_determinism: bool = False):

I like full_determinism a bit better. Since this is a new addition, the default should be set to False. Although it does fix what one might consider a bug, so I'm not sure on this one. @LysandreJik do you have an opinion?

"""
Helper function for reproducible behavior to set the seed in `random`, `numpy`, `torch` and/or `tf` (if installed).

Expand All @@ -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()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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:
Expand Down