This function returns a decorator intended to be applied to test methods in
a test_case.TestCase class. Doing so will cause the contents of the test
method to be executed twice - once in graph mode, and once with eager
execution enabled. This allows unittests to confirm the equivalence between
eager and graph execution.
This test validates that tf.add() has the same behavior when computed with
eager execution enabled as it does when constructing a TensorFlow graph and
executing the z tensor with a session.
Args
func
function to be annotated. If func is None, this method returns a
decorator the can be applied to a function. If func is not None this
returns the decorator applied to func.
config
An optional config_pb2.ConfigProto to use to configure the session
when executing graphs.
use_gpu
If True, attempt to run as many operations as possible on GPU.
Returns
Returns a decorator that will run the decorated test method twice:
once by constructing and executing a graph in a session and once with
eager execution enabled.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tfds.testing.run_in_graph_and_eager_modes\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/testing/test_utils.py#L351-L423) |\n\nExecute the decorated test in both graph mode and eager mode. \n\n tfds.testing.run_in_graph_and_eager_modes(\n func=None, config=None, use_gpu=True\n )\n\nThis function returns a decorator intended to be applied to test methods in\na `test_case.TestCase` class. Doing so will cause the contents of the test\nmethod to be executed twice - once in graph mode, and once with eager\nexecution enabled. This allows unittests to confirm the equivalence between\neager and graph execution.\n| **Note:** This decorator can only be used when executing eagerly in the outer scope.\n\nFor example, consider the following unittest: \n\n class SomeTest(tfds.testing.TestCase):\n\n @tfds.testing.run_in_graph_and_eager_modes\n def test_foo(self):\n x = tf.constant([1, 2])\n y = tf.constant([3, 4])\n z = tf.add(x, y)\n self.assertAllEqual([4, 6], self.evaluate(z))\n\n if __name__ == '__main__':\n tfds.testing.test_main()\n\nThis test validates that [`tf.add()`](https://www.tensorflow.org/api_docs/python/tf/math/add) has the same behavior when computed with\neager execution enabled as it does when constructing a TensorFlow graph and\nexecuting the `z` tensor with a session.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `func` | function to be annotated. If `func` is None, this method returns a decorator the can be applied to a function. If `func` is not None this returns the decorator applied to `func`. |\n| `config` | An optional config_pb2.ConfigProto to use to configure the session when executing graphs. |\n| `use_gpu` | If True, attempt to run as many operations as possible on GPU. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Returns a decorator that will run the decorated test method twice: once by constructing and executing a graph in a session and once with eager execution enabled. ||\n\n\u003cbr /\u003e"]]