MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / run_in_graph_and_eager_modes

Function run_in_graph_and_eager_modes

tensorflow/python/framework/test_util.py:1007–1116  ·  view source on GitHub ↗

Execute the decorated test with and without enabling eager execution. This function returns a decorator intended to be applied to test methods in a `tf.test.TestCase` class. Doing so will cause the contents of the test method to be executed twice - once normally, and once with eager execution

(func=None,
                                 config=None,
                                 use_gpu=True,
                                 reset_test=True,
                                 assert_no_eager_garbage=False)

Source from the content-addressed store, hash-verified

1005
1006
1007def run_in_graph_and_eager_modes(func=None,
1008 config=None,
1009 use_gpu=True,
1010 reset_test=True,
1011 assert_no_eager_garbage=False):
1012 """Execute the decorated test with and without enabling eager execution.
1013
1014 This function returns a decorator intended to be applied to test methods in
1015 a `tf.test.TestCase` class. Doing so will cause the contents of the test
1016 method to be executed twice - once normally, and once with eager execution
1017 enabled. This allows unittests to confirm the equivalence between eager
1018 and graph execution (see `tf.compat.v1.enable_eager_execution`).
1019
1020 For example, consider the following unittest:
1021
1022 ```python
1023 class MyTests(tf.test.TestCase):
1024
1025 @run_in_graph_and_eager_modes
1026 def test_foo(self):
1027 x = tf.constant([1, 2])
1028 y = tf.constant([3, 4])
1029 z = tf.add(x, y)
1030 self.assertAllEqual([4, 6], self.evaluate(z))
1031
1032 if __name__ == "__main__":
1033 tf.test.main()
1034 ```
1035
1036 This test validates that `tf.add()` has the same behavior when computed with
1037 eager execution enabled as it does when constructing a TensorFlow graph and
1038 executing the `z` tensor in a session.
1039
1040 `deprecated_graph_mode_only`, `run_v1_only`, `run_v2_only`, and
1041 `run_in_graph_and_eager_modes` are available decorators for different
1042 v1/v2/eager/graph combinations.
1043
1044
1045 Args:
1046 func: function to be annotated. If `func` is None, this method returns a
1047 decorator the can be applied to a function. If `func` is not None this
1048 returns the decorator applied to `func`.
1049 config: An optional config_pb2.ConfigProto to use to configure the session
1050 when executing graphs.
1051 use_gpu: If True, attempt to run as many operations as possible on GPU.
1052 reset_test: If True, tearDown and SetUp the test case between the two
1053 executions of the test (once with and once without eager execution).
1054 assert_no_eager_garbage: If True, sets DEBUG_SAVEALL on the garbage
1055 collector and asserts that no extra garbage has been created when running
1056 the test with eager execution enabled. This will fail if there are
1057 reference cycles (e.g. a = []; a.append(a)). Off by default because some
1058 tests may create garbage for legitimate reasons (e.g. they define a class
1059 which inherits from `object`), and because DEBUG_SAVEALL is sticky in some
1060 Python interpreters (meaning that tests which rely on objects being
1061 collected elsewhere in the unit test file will not work). Additionally,
1062 checks that nothing still has a reference to Tensors that the test
1063 allocated.
1064

Callers

nothing calls this directly

Calls 1

decoratorFunction · 0.70

Tested by

no test coverage detected