Hugging-Face-Supporter / tftokenizers

Use Huggingface Transformer and Tokenizers as Tensorflow Reusable SavedModels
Apache License 2.0
9 stars 4 forks source link

Can't reproduce the exampe in the Readme #21

Open IlnarSelimcan opened 1 year ago

IlnarSelimcan commented 1 year ago

Hi,

first of all, thanks for sharing this code!

When I copy-paste the example from the Examples section of the Readme, the line output = reloaded_model([s1, s2, s3]) causes the following error:

In [16]: output = reloaded_model([s1, s2, s3])
    ...: 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-977dbd3103dd> in <cell line: 1>()
----> 1 output = reloaded_model([s1, s2, s3])

~/.cache/pypoetry/virtualenvs/tftokenizers-F6-m1WpK-py3.8/lib/python3.8/site-packages/tensorflow/python/saved_model/load.py in _call_attribute(instance, *args, **kwargs)
    699 
    700 def _call_attribute(instance, *args, **kwargs):
--> 701   return instance.__call__(*args, **kwargs)
    702 
    703 

~/.cache/pypoetry/virtualenvs/tftokenizers-F6-m1WpK-py3.8/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
    151     except Exception as e:
    152       filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153       raise e.with_traceback(filtered_tb) from None
    154     finally:
    155       del filtered_tb

~/.cache/pypoetry/virtualenvs/tftokenizers-F6-m1WpK-py3.8/lib/python3.8/site-packages/tensorflow/python/saved_model/function_deserialization.py in restored_function_body(*args, **kwargs)
    261     """Calls a restored function or raises an error if no matching function."""
    262     if not saved_function.concrete_functions:
--> 263       raise ValueError("Found zero restored functions for caller function.")
    264     # This is the format of function.graph.structured_input_signature. At this
    265     # point, the args and kwargs have already been canonicalized.

ValueError: Found zero restored functions for caller function.

Have you guys ever encountered that before?

IlnarSelimcan commented 1 year ago

Packages were installed this way:

## git clone https://github.com/Hugging-Face-Supporter/tftokenizers.git
## cd tftokenizers
## conda create -n py38 python=3.8
## conda activate py38
## curl -sSL https://install.python-poetry.org/ | python3 -
## poetry env use `which python3.8`
## poetry shell
## poetry install
IlnarSelimcan commented 1 year ago

This looks relevant: https://www.tensorflow.org/api_docs/python/tf/saved_model/save#example_3 ?

IlnarSelimcan commented 1 year ago

The following change made the error go away:

selimcan@patroclus:/tmp/tft/tftokenizers$ git diff
diff --git a/tftokenizers/model.py b/tftokenizers/model.py
index 1d91bca..ef77ea0 100644
--- a/tftokenizers/model.py
+++ b/tftokenizers/model.py
@@ -39,6 +39,7 @@ class TFModel(keras.layers.Layer):
         self.tokenizer = tokenizer
         self.model = model

+    @tf.function(input_signature=(tf.TensorSpec(shape=[None], dtype=tf.string),))
     def call(
         self,
         inputs,