Skip to content

improve py_func#15121

Merged
caisq merged 8 commits intotensorflow:masterfrom
boeddeker:py_func
Jan 13, 2018
Merged

improve py_func#15121
caisq merged 8 commits intotensorflow:masterfrom
boeddeker:py_func

Conversation

@boeddeker
Copy link
Copy Markdown
Contributor

See #14448
Improved py_func to accept nested structures as input and as output like in tf.data.Dataset.form_generator

  • relable inp to args and Tout to output_types
  • add arguments kwargs and output_shapes
  • allow args/kwargs/output_types/output_shapes to be a nested structure

Open questions:

  • allow output_types to be callable? (Dynamic output_types inference from args/kwargs)
  • (new) argument names
  • backward compatibility for old names

 - relable inp to args and Tout to output_types
 - add arguments kwargs and output_shapes
 - allow args/kwargs/output_types/output_shapes to be a nested structure
@tensorflow-jenkins
Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@caisq caisq requested a review from alextp December 5, 2017 14:34
@caisq caisq self-assigned this Dec 5, 2017
@caisq caisq added the awaiting review Pull request awaiting review label Dec 5, 2017
@caisq
Copy link
Copy Markdown
Contributor

caisq commented Dec 7, 2017

ping @alextp

@alextp alextp requested a review from akshayka December 7, 2017 22:24
Comment thread tensorflow/python/ops/script_ops.py Outdated


"""
from tensorflow.python.framework import tensor_shape
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should go at the top of the file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move it to the top in the next commit.

Comment thread tensorflow/python/ops/script_ops.py Outdated


def py_func(func, inp, Tout, stateful=True, name=None):
def py_func(func, args=(), kwargs={}, output_types=None, output_shapes=None,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change from imp to args, kwargs can break existing code, breaking our API compatibility guidelines we've had since tf 1.0.

So we need to find a way of doing what you want while not changing the signature of py_func.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When my suggestion does not move to contrib or 3-party and it is necessary to keep the API, my suggestion is (I do not know the rules for deprecated arguments in tf, I saw only some examples):
def py_func(func, inp, Tout=None, stateful=True, name=None, kwargs={}, Sout=None)

What I want with the changed signature is to allow keyword arguments and automatic set_shape like in tf.data.Dataset.from_generator.

Since in python it is common to use func(*args, **kwargs) I used that names. output_types and shape came from tf.data.Dataset.from_generator.

Comment thread tensorflow/python/ops/script_ops.py Outdated
# If callable, assume same signature and call with tensors and get the shapes
output_shapes = output_shapes(*args, **kwargs)

flat_output_types = nest.flatten(output_types)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So my understanding is that the main change here is you call nest.flatten and nest.pack_sequence_as around the user code in a py_func. Everything else is debugging / diagnostics, right?

If so, I think this can live in utility code instead of in the tensorflow library proper. Maybe you want to add a contrib.py_func with this API or add this py func wrapper in a third-party library somewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. The main changed are allow kwargs as input and arbitrary returns with shapes (earlier only list/scalar without shape).
Another (not so important) point is to allow shape/type inference from the input.(Useful for decorators)
If you prefer contrib.py_func I can move the code to contrib.
If you prefer a third-party library, I will keep the code in a private repo.

When you need a use case inside of tf for this modification, look in tf.data.Dataset.from_generator.
The code there was the starting point.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the dataset API for py_func is friendlier to use and I like exposing it. We could consider either tf.py_func_v2, or contrib.

@martinwicke for API review as to whether we prefer _v2 or contrib

@martinwicke
Copy link
Copy Markdown
Member

I'd say contrib to start, so we can fix things if we find something wrong with the initial version.

@boeddeker
Copy link
Copy Markdown
Contributor Author

Where should the code be? I do not know how contrib is organized.

@alextp
Copy link
Copy Markdown
Contributor

alextp commented Dec 10, 2017

contrib.framework seems to be the least-inappropriate existing package. Just move this definition to a file there and it should work I think

@boeddeker
Copy link
Copy Markdown
Contributor Author

ok, I moved now the code to tensorflow/conrtib/framework/ops/session_ops.py

Copy link
Copy Markdown
Contributor

@alextp alextp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to import the symbol from contrib/framework/init.py or it won't show up in the public package

@boeddeker
Copy link
Copy Markdown
Contributor Author

sorry that I forgot the import, I fixed that.
There are maybe some more such bugs because I have not figured out how to install tensorflow from binary with git in editable mode.

@alextp
Copy link
Copy Markdown
Contributor

alextp commented Dec 11, 2017

Jenkins, test this please.

@alextp alextp added awaiting testing (then merge) and removed awaiting review Pull request awaiting review labels Dec 11, 2017
@alextp
Copy link
Copy Markdown
Contributor

alextp commented Dec 11, 2017 via email

@yifeif
Copy link
Copy Markdown
Contributor

yifeif commented Dec 19, 2017

@tensorflow-jenkins test this please

@yifeif yifeif added the kokoro:force-run Tests on submitted change label Dec 19, 2017
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Dec 19, 2017
@yifeif
Copy link
Copy Markdown
Contributor

yifeif commented Dec 21, 2017

@boeddeker do you mind fix the lint errors and test failures?

@yifeif yifeif added the stat:awaiting response Status - Awaiting response from author label Dec 21, 2017
@boeddeker
Copy link
Copy Markdown
Contributor Author

@yifeif Where do I find the lint errors and test failures?
Since I did not write a test, how could a test fail, that is related to this PR?
I only found one lint error.

@alextp
Copy link
Copy Markdown
Contributor

alextp commented Dec 21, 2017 via email

@yifeif yifeif added kokoro:force-run Tests on submitted change and removed stat:awaiting response Status - Awaiting response from author labels Dec 21, 2017
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Dec 21, 2017
from tensorflow.python.framework import tensor_shape
from tensorflow.python.util import nest

from tensorflow.python.ops.script_ops import py_func as _py_func
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the hidden ops for that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with hidden ops? I used a renaming because of the name collision.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@drpngx this is fine -- it's a contrib-only extension of py_func, py_func still exists as a public symbol, so we don't need hidden_ops.

@drpngx
Copy link
Copy Markdown
Contributor

drpngx commented Dec 26, 2017

This appears to make a lot of tests fail. Could you check?

@drpngx drpngx added the stat:awaiting response Status - Awaiting response from author label Dec 26, 2017
@boeddeker
Copy link
Copy Markdown
Contributor Author

I am not familiar with the import machinery in tensorflow, can someone explain, why the import does not work?
from tensorflow.contrib.framework.python.ops.script_ops import *

I will start to write tests, when the source code is formally correct for tensorflow.

@martinwicke
Copy link
Copy Markdown
Member

The @@py_func callout needs to go into the docstring of tensorflow/contrib/framework/__init__.py, since it is there that remove_undocumented is called.

Otherwise I think this should work.

@drpngx
Copy link
Copy Markdown
Contributor

drpngx commented Dec 27, 2017

@martinwicke Right, contrib, good point.

@drpngx
Copy link
Copy Markdown
Contributor

drpngx commented Dec 28, 2017

@boeddeker you're probably missing some dependency.

FAIL: //tensorflow/python/kernel_tests:rnn_test (shard 10 of 10) (see /tmpfs/tmp/bazel/execroot/org_tensorflow/bazel-out/k8-py3-opt/testlogs/tensorflow/python/kernel_tests/rnn_test/shard_10_of_10/test.log)
INFO: From Testing //tensorflow/python/kernel_tests:rnn_test (shard 10 of 10):
==================== Test output for //tensorflow/examples/image_retraining:retrain_test:
Traceback (most recent call last):
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/examples/image_retraining/retrain_test.py", line 24, in <module>
    from tensorflow.examples.image_retraining import retrain
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/examples/image_retraining/retrain.py", line 117, in <module>
    from tensorflow.contrib.quantize.python import quant_ops
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/contrib/__init__.py", line 22, in <module>
    from tensorflow.contrib import bayesflow
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/contrib/bayesflow/__init__.py", line 24, in <module>
    from tensorflow.contrib.bayesflow.python.ops import csiszar_divergence
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/contrib/bayesflow/python/ops/csiszar_divergence.py", line 26, in <module>
    from tensorflow.contrib.bayesflow.python.ops.csiszar_divergence_impl import *
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/contrib/bayesflow/python/ops/csiszar_divergence_impl.py", line 43, in <module>
    from tensorflow.contrib import framework as contrib_framework
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/contrib/framework/__init__.py", line 93, in <module>
    from tensorflow.contrib.framework.python.ops import *
  File "/tmp/botexec/bazel-out/k8-py3-opt/bin/tensorflow/examples/image_retraining/retrain_test.runfiles/org_tensorflow/tensorflow/contrib/framework/python/ops/__init__.py", line 27, in <module>
    from tensorflow.contrib.framework.python.ops.script_ops import *
ModuleNotFoundError: No module named 'tensorflow.contrib.framework.python.ops.script_ops'
================================================================================```

@yifeif
Copy link
Copy Markdown
Contributor

yifeif commented Jan 8, 2018

@tensorflow-jenkins test this please

@yifeif yifeif added kokoro:force-run Tests on submitted change and removed stat:awaiting response Status - Awaiting response from author labels Jan 8, 2018
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 8, 2018
@boeddeker
Copy link
Copy Markdown
Contributor Author

Do I have to add the new file to tf_custom_op_py_library inside the BUILD file? (I did this in my last commit).

@drpngx
Copy link
Copy Markdown
Contributor

drpngx commented Jan 9, 2018

Jenkins, test this please.

@drpngx drpngx added the kokoro:force-run Tests on submitted change label Jan 9, 2018
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 9, 2018
@boeddeker
Copy link
Copy Markdown
Contributor Author

Sorry, but I have no bazel and did not know that I have to add dependencies.

@yifeif yifeif added the kokoro:force-run Tests on submitted change label Jan 12, 2018
@yifeif
Copy link
Copy Markdown
Contributor

yifeif commented Jan 12, 2018

@tensorflow-jenkins test this please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants