Repository: huggingface/transformers

Issue:
Unable to serialize/save TF2.3.1 RobertaSequenceClassification model to saved model format 
## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->
     
- `transformers` version: 3.3.1
- Platform: Linux-3.13.0-158-generic-x86_64-with-glibc2.10
- Python version: 3.8.5
- PyTorch version (GPU?): not installed (NA)
- Tensorflow version (GPU?): 2.3.0 (False)
- Using GPU in script?:  No
- Using distributed or parallel set-up in script?:  No

### Who can help

@LysandreJik 
@jplu

## Information

Model I am using (Bert, XLNet ...):

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)

### Steps to reproduce the behavior:

from transformers import RobertaTokenizer, TFRobertaForSequenceClassification
import tensorflow as tf
import wget
import os, sys
local_path = os.path.abspath(os.path.join(__file__, "..", "resources/"))
tokenizer = RobertaTokenizer.from_pretrained("roberta-large-mnli")
model = TFRobertaForSequenceClassification.from_pretrained("roberta-large-mnli")
tf.keras.models.save_model(model, local_path, overwrite=True, include_optimizer=False, save_format='tf')

### Error

`WARNING:tensorflow:From /opt/conda/lib/python3.8/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From /opt/conda/lib/python3.8/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.

TypeError                                 Traceback (most recent call last)
<ipython-input-4-1a9d4ccbf378> in <module>
      8 model = TFRobertaForSequenceClassification.from_pretrained("roberta-large-mnli")
      9 
---> 10 tf.keras.models.save_model(model, local_path, overwrite=True, include_optimizer=False, save_format='tf')

/opt/conda/lib/python3.8/site-packages/tensorflow/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
    131         model, filepath, overwrite, include_optimizer)
    132   else:
--> 133     saved_model_save.save(model, filepath, overwrite, include_optimizer,
    134                           signatures, options)
    135 

/opt/conda/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save.py in save(model, filepath, overwrite, include_optimizer, signatures, options)
     78     # we use the default replica context here.
     79     with distribution_strategy_context._get_default_replica_context():  # pylint: disable=protected-access
---> 80       save_lib.save(model, filepath, signatures, options)
     81 
     82   if not include_optimizer:

/opt/conda/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in save(obj, export_dir, signatures, options)
    973   meta_graph_def = saved_model.meta_graphs.add()
    974 
--> 975   _, exported_graph, object_saver, asset_info = _build_meta_graph(
    976       obj, export_dir, signatures, options, meta_graph_def)
    977   saved_model.saved_model_schema_version = constants.SAVED_MODEL_SCHEMA_VERSION

/opt/conda/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in _build_meta_graph(obj, export_dir, signatures, options, meta_graph_def)
   1073         function_aliases[fdef.name] = alias
   1074 
-> 1075   object_graph_proto = _serialize_object_graph(saveable_view,
   1076                                                asset_info.asset_index)
   1077   meta_graph_def.object_graph_def.CopyFrom(object_graph_proto)

/opt/conda/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in _serialize_object_graph(saveable_view, asset_file_def_index)
    718 
    719   for obj, obj_proto in zip(saveable_view.nodes, proto.nodes):
--> 720     _write_object_proto(obj, obj_proto, asset_file_def_index,
    721                         saveable_view.function_name_map)
    722   return proto

/opt/conda/lib/python3.8/site-packages/tensorflow/python/saved_model/save.py in _write_object_proto(obj, proto, asset_file_def_index, function_name_map)
    759           version=versions_pb2.VersionDef(
    760               producer=1, min_consumer=1, bad_consumers=[]),
--> 761           metadata=obj._tracking_metadata)
    762       # pylint:enable=protected-access
    763     proto.user_object.CopyFrom(registered_type_proto)

/opt/conda/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _tracking_metadata(self)
   3009   @property
   3010   def _tracking_metadata(self):
-> 3011     return self._trackable_saved_model_saver.tracking_metadata
   3012 
   3013   def _list_extra_dependencies_for_serialization(self, serialization_cache):

/opt/conda/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/base_serialization.py in tracking_metadata(self)
     52     # TODO(kathywu): check that serialized JSON can be loaded (e.g., if an
     53     # object is in the python property)
---> 54     return json_utils.Encoder().encode(self.python_properties)
     55 
     56   def list_extra_dependencies_for_serialization(self, serialization_cache):

/opt/conda/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/json_utils.py in encode(self, obj)
     42 
     43   def encode(self, obj):
---> 44     return super(Encoder, self).encode(_encode_tuple(obj))
     45 
     46 

/opt/conda/lib/python3.8/json/encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

/opt/conda/lib/python3.8/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/opt/conda/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/json_utils.py in default(self, obj)
     39       items = obj.as_list() if obj.rank is not None else None
     40       return {'class_name': 'TensorShape', 'items': items}
---> 41     return serialization.get_json_type(obj)
     42 
     43   def encode(self, obj):

/opt/conda/lib/python3.8/site-packages/tensorflow/python/util/serialization.py in get_json_type(obj)
     70     return obj.__wrapped__
     71 
---> 72   raise TypeError('Not JSON Serializable:', obj)

TypeError: ('Not JSON Serializable:', RobertaConfig {
  "_num_labels": 3,
  "architectures": [
    "RobertaForSequenceClassification"
  ],
  "attention_probs_dropout_prob": 0.1,
  "bos_token_id": 0,
  "eos_token_id": 2,
  "gradient_checkpointing": false,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 1024,
  "id2label": {
    "0": "CONTRADICTION",
    "1": "NEUTRAL",
    "2": "ENTAILMENT"
  },
  "initializer_range": 0.02,
  "intermediate_size": 4096,
  "label2id": {
    "CONTRADICTION": 0,
    "ENTAILMENT": 2,
    "NEUTRAL": 1
  },
  "layer_norm_eps": 1e-05,
  "max_position_embeddings": 514,
  "model_type": "roberta",
  "num_attention_heads": 16,
  "num_hidden_layers": 24,
  "pad_token_id": 1,
  "type_vocab_size": 1,
  "vocab_size": 50265
}
)
`


## Expected behavior

Save the model correctly  as a tf.keras model.




Discussion:

Resolving patch:
diff --git a/src/transformers/modeling_tf_longformer.py b/src/transformers/modeling_tf_longformer.py
--- a/src/transformers/modeling_tf_longformer.py
+++ b/src/transformers/modeling_tf_longformer.py
@@ -125,7 +125,7 @@ class TFLongformerEmbeddings(tf.keras.layers.Layer):
     """
 
     def __init__(self, config, **kwargs):
-        super().__init__(config, **kwargs)
+        super().__init__(**kwargs)
 
         self.padding_idx = 1
         self.vocab_size = config.vocab_size
diff --git a/src/transformers/modeling_tf_roberta.py b/src/transformers/modeling_tf_roberta.py
--- a/src/transformers/modeling_tf_roberta.py
+++ b/src/transformers/modeling_tf_roberta.py
@@ -70,7 +70,7 @@ class TFRobertaEmbeddings(tf.keras.layers.Layer):
     """
 
     def __init__(self, config, **kwargs):
-        super().__init__(config, **kwargs)
+        super().__init__(**kwargs)
 
         self.padding_idx = 1
         self.vocab_size = config.vocab_size


Test patch: