transformers: Quick Tour TF2.0 error: dataclasses.FrozenInstanceError: cannot assign to field 'label'

🐛 Bug

Information

Model I am using: bert-base-cased

Language I am using the model on (English, Chinese …): English

The problem arises when using:

  • the official example scripts: (give details below)
  • my own modified scripts: (give details below)

The tasks I am working on is:

  • 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:
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

Environment info

  • 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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

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.

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 training_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!

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)

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