transformers icon indicating copy to clipboard operation
transformers copied to clipboard

Error while saving electra model in tensorflow "savedModel" format

Open nirajkale opened this issue 6 years ago • 5 comments

Environment info

  • transformers version: 3.0.2
  • Platform: Linux-5.3.0-1034-azure-x86_64-with-debian-buster-sid
  • Python version: 3.6.10
  • PyTorch version (GPU?): 1.5.1 (True)
  • Tensorflow version (GPU?): 2.0.0 (True)
  • Using GPU in script?: Yes (Tesla P40)
  • Using distributed or parallel set-up in script?: No

tensorflow: @jplu

Information

Model I am using "Electra" with TFElectraForQuestionAnswering:

The tasks I am working on is: I'm trying to use Pre-trained electra-large on squad 2.0 dataset. The model seems to be working fine. The problem arises when I treat it as a Keras model & try to save it in tensorflow's binary/SavedModel format. The reason I'm using SavedModel format is because I want to host the model using tensorflow model server.

To reproduce

Steps to reproduce the behavior:

1.Load the model with TFElectraForQuestionAnswering 2.Add input layers 3.save the model


from transformers import BertTokenizer, TFElectraForQuestionAnswering
import tensorflow as tf

model_name_or_path= 'ahotrod/electra_large_discriminator_squad2_512'
max_len = None

tokenizer = BertTokenizer.from_pretrained(model_name_or_path, cache_dir='transformers')
model = TFElectraForQuestionAnswering.from_pretrained(model_name_or_path, cache_dir='transformers')

input_ids = tf.keras.layers.Input(shape=(max_len,), name='input_ids', dtype='int32')
attention_mask = tf.keras.layers.Input(shape=(max_len,), name='attention_mask', dtype='int32')
token_type_ids = tf.keras.layers.Input(shape=(max_len,), name='token_type_ids', dtype='int32')
keras_input = [input_ids, attention_mask, token_type_ids]

qa_output = model(keras_input)
keras_model = tf.keras.Model(inputs= keras_input, outputs = qa_output)
print(keras_model.summary())

keras_model.save(r'exported/electra_large/0011')

When I run above snippet it gives error:

Exception has occurred: TypeError Expected Operation, Variable, or Tensor, got None File "/datadrive/users/niraj/qa/export_electra.py", line 31, in <module> keras_model.save(r'exported/electra_large/0011')

Expected behavior

Not able to figure out the reason for this but above snipper works if i use bert-large or any other model instead of electra. Probably something wrong with modeling scripts for electra ?

nirajkale avatar Aug 03 '20 09:08 nirajkale

Hey!

Sorry for the inconvenience, there are actually some issues with saved model, but we are currently working on several fix in this PR https://github.com/huggingface/transformers/pull/5468

jplu avatar Aug 03 '20 09:08 jplu

Thanks for quick response! Will wait for this PR to complete :-)

nirajkale avatar Aug 03 '20 09:08 nirajkale

The PR has been merged in master, can you retry and let us know if it is really fixed please? Otherwise I will work on it ^^

jplu avatar Aug 03 '20 12:08 jplu

Just pulled the latest code from master & now the SavedModel export is working fine for Electra 👍. Thanks a lot for your quick help! Closing the issue now.

nirajkale avatar Aug 03 '20 19:08 nirajkale

@nirajkale Hi, i find ur model's: ahotrod/electra_large_discriminator_squad2_512"". Is this model a finetune model, i mean you already used electra_large to finetune a model with your task right. thanks for your reply.

652994331 avatar Jan 07 '21 01:01 652994331