The TrainingArguments class was once mutable after creation. Now it is not:
transformers/training_args.py
def __setattr__(self, name, value):
# Once fully through the `__post_init__`, `TrainingArguments` are immutable
if not name.startswith("_") and getattr(self, "_frozen", False):
raise FrozenInstanceError(f"cannot assign to field {name}")
else:
super().__setattr__(name, value)
I configure the majority of my arguments from the command line, but I dynamically determine the output_dir at runtime. I also have certain scenarios where I need to dynamically adjust the batch size. Now none of this is possible anymore. This is pretty annoying. For now, my workaround is
object.__setattr__(training_args, "output_dir", "foo/bar/")
but this is rather annoying.
Why was this change made? Does anyone have a better solution?
1 Like
I am also not fully sure why this change was made.
The PR for the change is here:
huggingface:main
← huggingface:muellerzr-immutable
opened 12:44PM - 10 Aug 23 UTC
# What does this PR do?
This PR ensures that the `TrainingArguments` are a fu… lly immutable dataclass after the `__post_init__` has been ran. We'll find that the tests suddenly fail now 😉 Should be merged after https://github.com/huggingface/transformers/pull/25390
## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
[documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
[here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?
## 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.
@amyeroberts @sgugger
The recommended workaround seems to be to use dataclasses.replace as was done here
from dataclasses import replace
...
training_args = replace(training_args, **kwargs_to_set)
...
The TrainingArguments were never meant to be mutable, so if you truly need to change them, the solution there is the right one
Thanks for the dataclasses tip!
Is the immutability meant to make multiprocessing safer? Just curious, but I don’t see any reason off the top of my head why this is necessary.
A design paradigm, users really shouldn’t be messing with these at all, unless under specific circumstances