Add support for seed in DataCollatorForLanguageModeling
#36497
+248
−22
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.
What does this PR do?
This PR adds support for setting a seed in the class
DataCollatorForLanguageModeling
. This helps with reproducibility in generating masks for masked language modeling (MLM). This issue was approved by @Rocketknight1 (#36357)Currently, there is a way for ensuring reproducibility by using the function
transformers.set_seed()
. However, this function sets the seed of the global RNG for PyTorch, Numpy, etc. What this means is that, setting a global seed can impact other pseudo-random functions outside the scope of the collator such as parameter initialization for models. This also means, that changes in the script outside the collator can impact the masking.Instead, it is preferred to create generator objects which can be passed around to different functions. This is also considered good practice. What my PR does, is:
seed
parameter toDataCollatorForLanguageModeling
return_tensors
parameterThe generator object is scoped to the collator class, so it won't affect pseudo-random functions outside the class and vice-versa.
One important factor to consider is using multiple workers for the collator function, as PyTorch's
DataLoader
does. PyTorch has documentation regarding this, whereby we set a different seed for each worker given byshared_seed + worker_id
. This is because the worker's seeds would be cloned, and so each worker would mask the input in exactly the same manner, which is undesirable. A critical part of PyTorch'sDataLoader
is that from within the worker, it is possible to access the worker'sid
(important to set the worker seed). Because of this constraint, this PR only supports multi-worker scenarios with PyTorch'sDataLoader
. With theseed
set, if the code detects that the collator is running in a multi-processing scenario and the worker information is unavailable, an error is thrown.The algorithm for creating the generator object is:
Tests have also been written to verify this behaviour in
tests/trainer/test_data_collator.py
.These changes were done on Python 3.12.8. The dependencies installed were as pip install -e ".[dev]" along with:
Fixes #36357
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.
@Rocketknight1 should be the right person to review this.