From f37380b064948fb6dd45feef0e8d93130c2f9884 Mon Sep 17 00:00:00 2001 From: Akshay Agrawal Date: Thu, 7 Dec 2017 15:18:46 -0800 Subject: [PATCH] Add tfe.py_func, a tf.py_func-like construct that wraps a Python function and executes it eagerly. In particular, an EagerPyFunc op is added that wraps a Python function and executes it eagerly. The wrapped function should take Tensors as inputs and return Tensors as outputs. Because functions wrapped in an EagerPyFunc are executed eagerly, they can make use of TensorFlow operations. EagerPyFunc should be differentiable, in principle; a gradient will be implemented and registered in a future change. Once a gradient is implemented, tfe.py_func will probably be the easiest mechanism for experimenting with custom ops. tfe.py_func will also make it easier to translate python functions with side-effects into defun-able code. PiperOrigin-RevId: 178303818 --- tensorflow/contrib/eager/python/BUILD | 1 + tensorflow/contrib/eager/python/tfe.py | 3 + .../base_api/api_def_EagerPyFunc.pbtxt | 8 + .../python_api/api_def_EagerPyFunc.pbtxt | 4 + tensorflow/core/ops/script_ops.cc | 14 ++ tensorflow/python/BUILD | 4 + tensorflow/python/kernel_tests/BUILD | 2 + .../python/kernel_tests/py_func_test.py | 221 +++++++++++++----- tensorflow/python/lib/core/py_func.cc | 72 +++++- tensorflow/python/lib/core/py_func.h | 20 +- tensorflow/python/ops/hidden_ops.txt | 1 + tensorflow/python/ops/script_ops.py | 176 ++++++++++---- 12 files changed, 399 insertions(+), 127 deletions(-) create mode 100644 tensorflow/core/api_def/base_api/api_def_EagerPyFunc.pbtxt create mode 100644 tensorflow/core/api_def/python_api/api_def_EagerPyFunc.pbtxt diff --git a/tensorflow/contrib/eager/python/BUILD b/tensorflow/contrib/eager/python/BUILD index 6e9bb87d58d..fb667cd91bd 100644 --- a/tensorflow/contrib/eager/python/BUILD +++ b/tensorflow/contrib/eager/python/BUILD @@ -19,6 +19,7 @@ py_library( "//tensorflow/python:framework_test_lib", "//tensorflow/python:numerics", "//tensorflow/python:resource_variable_ops", + "//tensorflow/python:script_ops", "//tensorflow/python:util", "//tensorflow/python:variable_scope", "//tensorflow/python/eager:backprop", diff --git a/tensorflow/contrib/eager/python/tfe.py b/tensorflow/contrib/eager/python/tfe.py index 1697c879def..770a7e3e7a0 100644 --- a/tensorflow/contrib/eager/python/tfe.py +++ b/tensorflow/contrib/eager/python/tfe.py @@ -23,6 +23,7 @@ To use, at program startup, call `tfe.enable_eager_execution()`. @@list_devices @@num_gpus +@@py_func @@defun @@implicit_gradients @@implicit_value_and_gradients @@ -101,8 +102,10 @@ from tensorflow.python.framework.test_util import IsolateTest from tensorflow.python.framework.test_util import run_in_graph_and_eager_modes as run_test_in_graph_and_eager_modes from tensorflow.python.ops.resource_variable_ops import ResourceVariable as Variable from tensorflow.python.ops.variable_scope import EagerVariableStore +from tensorflow.python.ops import script_ops from tensorflow.python.util.all_util import remove_undocumented +py_func = script_ops.eager_py_func defun = function.defun implicit_gradients = backprop.implicit_grad implicit_value_and_gradients = backprop.implicit_val_and_grad diff --git a/tensorflow/core/api_def/base_api/api_def_EagerPyFunc.pbtxt b/tensorflow/core/api_def/base_api/api_def_EagerPyFunc.pbtxt new file mode 100644 index 00000000000..9231368e165 --- /dev/null +++ b/tensorflow/core/api_def/base_api/api_def_EagerPyFunc.pbtxt @@ -0,0 +1,8 @@ +op { + graph_op_name: "EagerPyFunc" + summary: "Eagerly executes a python function to compute func(input)->output. The" + description: <