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

[TF2] tf.saved_model.save does not support type annotations #26864

Closed
alanhdu opened this issue Mar 19, 2019 · 6 comments
Closed

[TF2] tf.saved_model.save does not support type annotations #26864

alanhdu opened this issue Mar 19, 2019 · 6 comments
Assignees
Labels
comp:runtime c++ runtime, performance issues (cpu) stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 TF 2.1 for tracking issues in 2.1 release type:support Support issues

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Mar 19, 2019

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
  • TensorFlow installed from (source or binary): pip wheel
  • TensorFlow version (use command below): tf-nightly-2.0-preview==2.0.0.dev20190318
  • Python version: 3.7

Describe the current behavior
Given the following code:

import tensorflow as tf

class A(tf.Module):
    @tf.function
    def func(self, x: int):
        pass


a = A() 
tf.saved_model.save(a, export_dir=".")

I get the following traceback:

Traceback (most recent call last):
  File "scratch.py", line 10, in <module>
    tf.saved_model.save(a, export_dir=".")
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py", line 812, in save
    saveable_view, asset_info.asset_index)
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py", line 561, in _serialize_object_graph
    _write_object_proto(obj, obj_proto, asset_file_def_index)
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py", line 577, in _write_object_proto
    function_serialization.serialize_function(obj))
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/function_serialization.py", line 81, in serialize_function
    function_spec_proto = _serialize_function_spec(function.function_spec, coder)
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/function_serialization.py", line 29, in _serialize_function_spec
    proto.fullargspec.CopyFrom(coder.encode_structure(function_spec.fullargspec))
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 85, in encode_structure
    return self._map_structure(nested_structure, self._get_encoders())
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 69, in _map_structure
    return do(pyobj, recursion_fn)
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 224, in do_encode
    pair.value.CopyFrom(encode_fn(named_tuple_value._asdict()[key]))
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 69, in _map_structure
    return do(pyobj, recursion_fn)
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 193, in do_encode
    encoded_dict.dict_value.fields[key].CopyFrom(encode_fn(value))
  File "/opt/anaconda/envs/tf2/lib/python3.7/site-packages/tensorflow/python/saved_model/nested_structure_coder.py", line 71, in _map_structure
    "No encoder for object [%s] of type [%s]." % (str(pyobj), type(pyobj)))
tensorflow.python.saved_model.nested_structure_coder.NotEncodableError: No encoder for object [<class 'int'>] of type [<class 'type'>].

Describe the expected behavior

Ideally, saving this would work just fine and not crash on the type annotation.

@allenlavoie
Copy link
Member

@vbardiovskyg do we need an extension of structure coding for type annotations? I guess stripping them would be slightly better than throwing an exception, but long-term it seems like we want to save them. We should be able to add a Python 3-only unit test suite.

@alanhdu
Copy link
Contributor Author

alanhdu commented Mar 19, 2019

One work-around for now is to use str annotations like:

def func(self, x: "int"):
    ...

In Python 3.7, doing

from __future__ import annotations

will also by-default make all the type annotations strings without having to manually quote them (this successfully saves the function without crashing).

@allenlavoie
Copy link
Member

Oh interesting, we do save annotations, we just don't have a case for "int". Seems easy to add.

On the other hand, passing non-Tensors (like Python "int"s) to tf.functions will re-trace each time (i.e. each value of the integer will get its own graph). Out of curiosity, what is the Python int argument there for?

@ymodak ymodak added the TF 2.0 Issues relating to TensorFlow 2.0 label Mar 19, 2019
@ymodak ymodak self-assigned this Mar 19, 2019
@ymodak ymodak added comp:runtime c++ runtime, performance issues (cpu) type:support Support issues stat:awaiting response Status - Awaiting response from author labels Mar 19, 2019
@ymodak ymodak assigned allenlavoie and unassigned ymodak Mar 19, 2019
@tensorflowbutler
Copy link
Member

We are closing this issue for now due to lack of activity. Please comment if this is still an issue for you. Thanks!

@depthwise
Copy link

I'm seeing this issue again in RC0 when I provide tf.Tensor type annotation in call(). The exact error is:

tensorflow.python.saved_model.nested_structure_coder.NotEncodableError: No encoder for object [<class 'tensorflow.python.framework.ops.Tensor'>] of type [<class 'type'>].

Removing type annotations resolves the issue. This is, quite obviously, not ideal, so I think the issue should be re-opened.

@mr-ubik
Copy link

mr-ubik commented Sep 11, 2019

I have the same bug as @depthwise. Definitely should be reopened.

@kkimdev kkimdev self-assigned this Oct 16, 2019
@kkimdev kkimdev reopened this Oct 16, 2019
@goldiegadde goldiegadde added the TF 2.1 for tracking issues in 2.1 release label Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:runtime c++ runtime, performance issues (cpu) stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 TF 2.1 for tracking issues in 2.1 release type:support Support issues
Projects
None yet
Development

No branches or pull requests

8 participants