Quick Tour TF2.0 error: dataclasses.FrozenInstanceError: cannot assign to field 'label'
Author: anhminh3105
Pull request: no

# 🐛 Bug

## Information

Model I am using: bert-base-cased

Language I am using the model on (English, Chinese ...): English

The problem arises when using:
* [x] the official example scripts: (give details below)
* [ ] my own modified scripts: (give details below)

The tasks I am working on is:
* [x] an official GLUE/SQUaD task: (give the name)
* [ ] my own task or dataset: (give details below)

## To reproduce

Steps to reproduce the behavior:

1. install TensorFlow 2 with conda  `conda install tensorflow`
2. install Transformers either from source or using pip `pip install transformers`
3. run the Quick Tour TF 2 example with the following content:
```python
import tensorflow as tf
import tensorflow_datasets
from transformers import *

# Load dataset, tokenizer, model from pretrained model/vocabulary
tokenizer = BertTokenizer.from_pretrained('bert-base-cased')
model = TFBertForSequenceClassification.from_pretrained('bert-base-cased')
data = tensorflow_datasets.load('glue/mrpc')

# Prepare dataset for GLUE as a tf.data.Dataset instance
train_dataset = glue_convert_examples_to_features(data['train'], tokenizer, max_length=128, task='mrpc')
valid_dataset = glue_convert_examples_to_features(data['validation'], tokenizer, max_length=128, task='mrpc')
train_dataset = train_dataset.shuffle(100).batch(32).repeat(2)
valid_dataset = valid_dataset.batch(64)

# Prepare training: Compile tf.keras model with optimizer, loss and learning rate schedule
optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5, epsilon=1e-08, clipnorm=1.0)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
model.compile(optimizer=optimizer, loss=loss, metrics=[metric])

# Train and evaluate using tf.keras.Model.fit()
history = model.fit(train_dataset, epochs=2, steps_per_epoch=115,
                    validation_data=valid_dataset, validation_steps=7)

# Load the TensorFlow model in PyTorch for inspection
model.save_pretrained('./save/')
```
## Expected behavior
```
Traceback (most recent call last):
  File "quick_tour_tf2.py", line 11, in <module>
    train_dataset = glue_convert_examples_to_features(data['train'], tokenizer, max_length=128, task='mrpc')
  File "C:\Users\Anh Minh\.conda\envs\transformers\lib\site-packages\transformers\data\processors\glue.py", line 86, in glue_convert_examples_to_features
    example = processor.tfds_map(example)
  File "C:\Users\Anh Minh\.conda\envs\transformers\lib\site-packages\transformers\data\processors\utils.py", line 115, in tfds_map
    example.label = self.get_labels()[int(example.label)]
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'label'
```
### Update:
I have recently installed Pytorch and tried out `examples/run_tf_glue.py` and the same error occured.
```
(transformers) C:\Users\Anh Minh\Workspace\transformers_my_codes>python run_tf_glue.py
2020-03-31 10:43:55.555102: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2
2020-03-31 10:44:02.576281: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-03-31 10:44:02.669572: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2080 computeCapability: 7.5
coreClock: 1.59GHz coreCount: 46 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.29GiB/s
2020-03-31 10:44:02.679337: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-03-31 10:44:02.683708: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-03-31 10:44:02.689044: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-03-31 10:44:02.693280: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-03-31 10:44:02.762552: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-03-31 10:44:02.767982: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-03-31 10:44:02.773095: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-03-31 10:44:02.779069: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-03-31 10:44:02.789070: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-03-31 10:44:02.799782: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2080 computeCapability: 7.5
coreClock: 1.59GHz coreCount: 46 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.29GiB/s
2020-03-31 10:44:02.809292: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-03-31 10:44:02.813862: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-03-31 10:44:02.818889: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-03-31 10:44:02.823516: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-03-31 10:44:02.828140: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-03-31 10:44:02.833958: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-03-31 10:44:02.839710: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-03-31 10:44:02.845469: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
2020-03-31 10:44:05.483986: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-03-31 10:44:05.489238: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102]      0
2020-03-31 10:44:05.492138: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0:   N
2020-03-31 10:44:05.499953: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6269 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2080, pci bus id: 0000:01:00.0, compute capability: 7.5)
2020-03-31 10:44:06.412558: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
INFO:absl:Overwrite dataset info from restored data version.
INFO:absl:Reusing dataset glue (C:\Users\Anh Minh\tensorflow_datasets\glue\mrpc\1.0.0)
INFO:absl:Constructing tf.data.Dataset for split None, from C:\Users\Anh Minh\tensorflow_datasets\glue\mrpc\1.0.0
Traceback (most recent call last):
  File "run_tf_glue.py", line 51, in <module>
    train_dataset = glue_convert_examples_to_features(data["train"], tokenizer, 128, TASK)
  File "C:\Users\Anh Minh\.conda\envs\transformers\lib\site-packages\transformers\data\processors\glue.py", line 86, in glue_convert_examples_to_features
    example = processor.tfds_map(example)
  File "C:\Users\Anh Minh\.conda\envs\transformers\lib\site-packages\transformers\data\processors\utils.py", line 115, in tfds_map
    example.label = self.get_labels()[int(example.label)]
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'label'
```

The issue has been resolved by reinstalling Transformers 2.5.0

<!-- A clear and concise description of what you would expect to happen. -->

## Environment info
<!-- You can run the command `python transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 2.7.0
- Platform: Windows 10
- Python version: 3.7.7
- PyTorch version (GPU?): 1.4.0 on GPU
- Tensorflow version (GPU?): 2.1 on GPU
- Using GPU in script?: yes, RTX 2080
- Using distributed or parallel set-up in script?: Unavailable

Comments:
This was fixed yesterday, can you try installing from master?

verified that it's fixed in master. However, the bug remains when installing from pip. 

We'll ship a new pip release soon, but in any case we'll try to update the code so that the TF script can run with an immutable `InputExample` (as discussed w/ @jplu)

I have just had the same problem. Can you show me how to fix it specifically?

[Install from source](https://github.com/huggingface/transformers#from-source)

I am executing finetune_llama2_guanaco_7b.sh using qlora. I am getting the below error. Attaching the stack trace.

qlora.py", line 841, in <module>
    train()
  qlora.py", line 694, in train
    training_args.generation_config = transformers.GenerationConfig(**vars(generation_args))
  File "qlora/.venv/lib/python3.10/site-packages/transformers/training_args.py", line 1714, in __setattr__
    raise FrozenInstanceError(f"cannot assign to field {name}")
dataclasses.FrozenInstanceError: cannot assign to field generation_config

Can you give pls suggest me a solution to fix this issue?

Are you using this [script](https://gist.github.com/younesbelkada/9f7f75c94bdc1981c8ca5cc937d4a4da)? Are you sure you are using the latest version of `transformers`? 

I am using the run_summarization.py to finetune and infer the mt5 model. 
The script is https://github.com/huggingface/transformers/blob/main/examples/pytorch/summarization/run_summarization.py
transformers_version is **4.32.0**

I get the below error:
 Traceback (most recent call last):
  File "./code/run_summarization.py", line 902, in <module>
    main()
  File "./code/run_summarization.py", line 764, in main
    t**raining_args.generation_max_length** = (
  File "/tmp/env/lib/python3.8/site-packages/transformers/training_args.py", line 1712, in __setattr__
    raise **FrozenInstanceError(f"cannot assign to field {name}")**
dataclasses.FrozenInstanceError: cannot assign to field generation_max_length

Could you please help look? Any help will be appreciated!


cc @muellerzr and @gante We have some needed updates in the script.

I am trying to use the run_mae.py to finetune an MAE model on my own dataset.
This is the referenced script: https://github.com/huggingface/transformers/blob/main/examples/pytorch/image-pretraining/run_mae.py
and the minimum version for the transformers is:
transformers_version is 4.32.0.dev0

I encountered the below error:
Traceback (most recent call last):
  File "run_mae_mydata.py", line 400, in <module>
    main()
  File "run_mae_mydata.py", line 351, in main
    training_args.learning_rate = training_args.base_learning_rate * total_train_batch_size / 256
  File "./miniconda3/envs/mae/lib/python3.8/site-packages/transformers/training_args.py", line 1712, in __setattr__
    raise FrozenInstanceError(f"cannot assign to field {name}")
dataclasses.FrozenInstanceError: cannot assign to field learning_rate

Any advice/help is appreciated.

After I downgraded transfromers from 4.32 to 4.31, this bug just went away. Hope it will help someone who meets this bug too.

@tuzeao @bgoldenboy please use the latest version of the script and let me know if you still face issues: https://github.com/huggingface/transformers/blob/main/examples/pytorch/image-pretraining/run_mae.py (this was fixed a week ago or two)

> @tuzeao @bgoldenboy please use the latest version of the script and let me know if you still face issues: https://github.com/huggingface/transformers/blob/main/examples/pytorch/image-pretraining/run_mae.py (this was fixed a week ago or two)

Thank you for your reply. I used the updated script and it works now! Thanks again!

Same problem here when upgrading transformers 4.32->4.31

This has been reverted on main @ndvbd, either install via `pip install git+https://github.com/huggingface/transformers`, or use the version of the script I linked

The whole frozen arguments thing was removed in main? I'm trying to change seed, and few more.

Yes it was. See https://github.com/huggingface/transformers/pull/25903 (which discusses why)

> I am executing finetune_llama2_guanaco_7b.sh using qlora. I am getting the below error. Attaching the stack trace.
> 
> qlora.py", line 841, in train() qlora.py", line 694, in train training_args.generation_config = transformers.GenerationConfig(**vars(generation_args)) File "qlora/.venv/lib/python3.10/site-packages/transformers/training_args.py", line 1714, in **setattr** raise FrozenInstanceError(f"cannot assign to field {name}") dataclasses.FrozenInstanceError: cannot assign to field generation_config
> 
> Can you give pls suggest me a solution to fix this issue?

Change:
```
training_args.generation_config = transformers.GenerationConfig(**vars(generation_args)) 
```
To:
```
import dataclasses
import transformers
training_args = dataclasses.replace(
    training_args,
    generation_config = transformers.GenerationConfig(**vars(generation_args))
)
````
In case of updating, e.g., max_length: 
```
# training_args.max_length = 1024 # got error
training_args = dataclasses.replace(training_args, max_length=1024)
```