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

tf.function raises OSError "could not get source code" when run in a Python shell #24759

Closed
ageron opened this issue Jan 8, 2019 · 5 comments
Assignees
Labels
comp:autograph Autograph related issues TF 1.13 Issues related to TF 1.13

Comments

@ageron
Copy link
Contributor

ageron commented Jan 8, 2019

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
    Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
    Mac OS X 10.13.6
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
    N/A
  • TensorFlow installed from (source or binary):
    binary
  • TensorFlow version (use command below):
    VERSION="1.13.0-dev20181226" (this is the TF 2.0-preview)
    GIT_VERSION="b'v1.12.0-5133-gc343196842'"
  • Python version:
    3.6.6
  • Bazel version (if compiling from source):
    N/A
  • GCC/Compiler version (if compiling from source):
    N/A
  • CUDA/cuDNN version:
    N/A
  • GPU model and memory:
    N/A

Describe the current behavior
tf.function raises an OSError exception with the message "could not get source code" when I run it in a Python shell. The error does not occur in Jupyter or ipython, and it does not occur when I revert back to TF 1.12.0.

Describe the expected behavior
I expect no error.

Code to reproduce the issue

Run this in a Python shell:

import tensorflow as tf

@tf.function
def foo():
    return 42

foo()

Other info / logs

Here is the output of the above commands when I run them in a Python shell:

(tf2) $ python
Python 3.6.6 (default, Jun 28 2018, 05:43:53)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> @tf.function
... def foo():
...     return 42
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 336, in __call__
    self._initialize(args, kwds)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 309, in _initialize
    *args, **kwds))
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 1024, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 1247, in _maybe_define_function
    arg_names=arg_names),
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 456, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 261, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 449, in wrapper
    ), *args, **kwargs)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 293, in converted_call
    experimental_partial_types=partial_types)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 415, in to_graph
    arg_values, arg_types)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/autograph/impl/conversion.py", line 175, in entity_to_graph
    node, name, ns = function_to_graph(o, program_ctx, arg_values, arg_types)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/autograph/impl/conversion.py", line 338, in function_to_graph
    node, source = parser.parse_entity(f)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/autograph/pyct/parser.py", line 34, in parse_entity
    source = tf_inspect.getsource(entity)
  File "/Users/ageron/.virtualenvs/tf2/lib/python3.6/site-packages/tensorflow/python/util/tf_inspect.py", line 327, in getsource
    return _inspect.getsource(tf_decorator.unwrap(object)[1])
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 968, in getsource
    lines, lnum = getsourcelines(object)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 955, in getsourcelines
    lines, lnum = findsource(object)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/inspect.py", line 786, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

Below is the output when I run the exact same commands in an ipython shell (within the same virtualenv):

(tf2) $ ipython
Python 3.6.6 (default, Jun 28 2018, 05:43:53)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import tensorflow as tf

In [2]: @tf.function
   ...: def foo():
   ...:     return 42
   ...:

In [3]: foo()
2019-01-08 13:02:33.785802: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Out[3]: <tf.Tensor: id=5, shape=(), dtype=int32, numpy=42>

The result is also good in Jupyter (within the same virtualenv).

This is the output when I switch to another virtualenv based on TF 1.12.0:

(tf1) $ python
Python 3.6.6 (default, Jun 28 2018, 05:43:53)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.enable_eager_execution()
>>> @tf.contrib.eager.defun
... def foo():
...     return 42
...
>>> foo()
2019-01-08 12:55:53.087099: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
<tf.Tensor: id=5, shape=(), dtype=int32, numpy=42>
@ymodak ymodak self-assigned this Jan 8, 2019
@ymodak ymodak added the TF 2.0 Issues relating to TensorFlow 2.0 label Jan 8, 2019
@ymodak ymodak assigned martinwicke and unassigned ymodak Jan 8, 2019
@martinwicke
Copy link
Member

I believe this is an autograph issue?

@mdanatg
Copy link

mdanatg commented Jan 12, 2019

Yes, it's a currently known limitation. Libraries like dill seem to be are able to get past that, we'll look into those.

In the mean time, you can use @tf.function(autograph=False) inside a Python shell, but that means you'll have to use graph code.

We could revert to autograph=False automatically and output a warning in cases like this, I wonder if that would be a better user experience?

@mdanatg mdanatg added the comp:autograph Autograph related issues label Jan 12, 2019
@ageron
Copy link
Contributor Author

ageron commented Jan 12, 2019

Thanks for your feedback @mdanatg. Indeed, until a fix is found, I like the idea of reverting to autograph=False and outputting a warning. Or perhaps simply having a more explicit error message?

@mdanatg
Copy link

mdanatg commented Jan 12, 2019

Agreed, we'll definitely add a better error message.

tensorflow-copybara pushed a commit that referenced this issue Jan 25, 2019
@jvishnuvardhan jvishnuvardhan added TF 1.13 Issues related to TF 1.13 and removed TF 2.0 Issues relating to TensorFlow 2.0 labels Jan 31, 2019
@alextp
Copy link
Contributor

alextp commented Feb 22, 2019

We now revert to autograph=False, so closing this issue for now.

@alextp alextp closed this as completed Feb 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:autograph Autograph related issues TF 1.13 Issues related to TF 1.13
Projects
None yet
Development

No branches or pull requests

6 participants