Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BERT TF-Lite conversion not working in TensorFlow 2.2.0 #3905

Closed
r4ghu opened this issue Apr 22, 2020 · 1 comment
Closed

BERT TF-Lite conversion not working in TensorFlow 2.2.0 #3905

r4ghu opened this issue Apr 22, 2020 · 1 comment
Labels

Comments

@r4ghu
Copy link

r4ghu commented Apr 22, 2020

🐛 Bug

Model conversion succeeds but inputs and outputs are not recognized.

Information

Model I am using (Bert, XLNet ...): BERT (bert-base-uncased)

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

The problem arises when using:

  • TensorFlow 2.1.0 + Transformers 2.8.0 - has no problem converting bert-base-uncased model to tflite version.
  • TensorFlow 2.2.0rc3 + Transformers 2.8.0 - has issues with interoperability.

The tasks I am working on is:

  • Convert BERT models to TF-Lite format to use it in mobile apps.
  • Trying to use the latest TF-Lite package version for Android in the place of TF-Lite package provided in the repo huggingface/tflite-android-transformers.

To reproduce

Please execute the following code with TensorFlow versions 2.1.0 and 2.2.0-rc3

import transformers
from transformers import TFBertModel, BertConfig
import tensorflow as tf
print('TensorFlow version =', tf.__version__)
print('Transformers version =', transformers.__version__)

MODEL_DIR = 'bert-base-uncased'
MAX_SEQ_LEN = 50

# Read the model
config = BertConfig.from_pretrained(MODEL_DIR)
model = TFBertModel(config)


# Set input Spec
input_spec = [
    tf.TensorSpec([1, MAX_SEQ_LEN], tf.int32),
    tf.TensorSpec([1, MAX_SEQ_LEN], tf.int32),
    tf.TensorSpec([1, MAX_SEQ_LEN], tf.int32)
]

model._set_inputs(input_spec, training=False)

print(model.inputs)
print(model.outputs)
  • For TensorFlow 2.2.0-rc3: Model outputs and inputs are None
TensorFlow version = 2.2.0-rc3
Transformers version = 2.8.0
None
None
  • For TensorFlow 2.1.0:
TensorFlow version = 2.1.0
Transformers version = 2.8.0
...
[<tf.Tensor 'input_1:0' shape=(None, 50) dtype=int32>, <tf.Tensor 'input_2:0' shape=(None, 50) dtype=int32>, <tf.Tensor 'input_3:0' shape=(None, 50) dtype=int32>]
[<tf.Tensor 'tf_bert_model/Identity:0' shape=(None, 50, 768) dtype=float32>, <tf.Tensor 'tf_bert_model/Identity_1:0' shape=(None, 768) dtype=float32>]

Expected behavior

  • I expect the BERT model conversion to work properly to TensorFlow 2.2.0-rc{1/2/3}
  • Preferably BERT should use the default TF-Lite supported layers just like MobileBERT model provided by Google.
  • Image - MobileBERT from Google's bert-qa android example (left) vs BERT converted using the above script using TensorFlow v2.1.0 (right)
    MiniLM vs BERT

Environment info

  • transformers version: 2.8.0
  • Platform: Windows
  • Python version: 3.7.7
  • PyTorch version (GPU?): 1.4.0 (No GPU)
  • Tensorflow version (GPU?): 2.1.0 (working), 2.2.0-rc3 (not working) (no GPU for both versions)
  • Using GPU in script?: No
  • Using distributed or parallel set-up in script?: No
@stale
Copy link

stale bot commented Jun 21, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 21, 2020
@r4ghu r4ghu closed this as completed Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant