# TrainingArguments now Immutable. Why?

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainingarguments-now-immutable-why%2F52565%3E%5C
**Category:** 🤗Transformers\
**Created:** [August 28, 2023, 11:20pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainingarguments-now-immutable-why%2F52565 "2023-08-28T23:20:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lkurlandski](https://avatars.discourse-cdn.com/v4/letter/l/b782af/32.png) [@lkurlandski](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flkurlandski)%5C
**Post date:** [August 28, 2023, 11:20pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainingarguments-now-immutable-why%2F52565%2F1 "2023-08-28T23:20:30Z")

</div>

The TrainingArguments class was once mutable after creation. Now it is not:

transformers/training\_args.py

```auto
    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

```auto
object. __setattr__ (training_args, "output_dir", "foo/bar/")

```

but this is rather annoying.

Why was this change made? Does anyone have a better solution?

---

<div class="post-metadata">

**Author:** ![Jackmin108](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/jackmin108/32/18918_2.png) [@Jackmin108](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJackmin108)%5C
**Post date:** [August 30, 2023, 12:31am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainingarguments-now-immutable-why%2F52565%2F2 "2023-08-30T00:31:58Z")

</div>

I am also not fully sure why this change was made.  
The PR for the change is here:

> <https://github.com/huggingface/transformers/pull/25435>
>
> \# 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\](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2F)%3F 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](https://github.com/huggingface/trl/pull/682)

```python
from dataclasses import replace
...
training_args = replace(training_args, **kwargs_to_set)
...

```

---

<div class="post-metadata">

**Author:** ![muellerzr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muellerzr/32/21583_2.png) [@muellerzr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmuellerzr)%5C
**Post date:** [August 30, 2023, 2:33am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainingarguments-now-immutable-why%2F52565%2F3 "2023-08-30T02:33:21Z")

</div>

The TrainingArguments were never meant to be mutable, so if you truly need to change them, the solution there is the right one

---

<div class="post-metadata">

**Author:** ![lkurlandski](https://avatars.discourse-cdn.com/v4/letter/l/b782af/32.png) [@lkurlandski](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flkurlandski)%5C
**Post date:** [August 30, 2023, 12:51pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainingarguments-now-immutable-why%2F52565%2F4 "2023-08-30T12:51:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![muellerzr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muellerzr/32/21583_2.png) [@muellerzr](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmuellerzr)%5C
**Post date:** [August 30, 2023, 12:57pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainingarguments-now-immutable-why%2F52565%2F5 "2023-08-30T12:57:05Z")

</div>

> [@lkurlandski](#):
>
> 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
