Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/data/ops/debug_mode.py: 64%

11 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2022 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""Python debug mode enabler.""" 

16 

17from tensorflow.python.eager import context 

18from tensorflow.python.util.tf_export import tf_export 

19 

20 

21DEBUG_MODE = False 

22 

23 

24@tf_export("data.experimental.enable_debug_mode") 

25def enable_debug_mode(): 

26 """Enables debug mode for tf.data. 

27 

28 Example usage with pdb module: 

29 ``` 

30 import tensorflow as tf 

31 import pdb 

32 

33 tf.data.experimental.enable_debug_mode() 

34 

35 def func(x): 

36 # Python 3.7 and older requires `pdb.Pdb(nosigint=True).set_trace()` 

37 pdb.set_trace() 

38 x = x + 1 

39 return x 

40 

41 dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) 

42 dataset = dataset.map(func) 

43 

44 for item in dataset: 

45 print(item) 

46 ``` 

47 

48 The effect of debug mode is two-fold: 

49 

50 1) Any transformations that would introduce asynchrony, parallelism, or 

51 non-determinism to the input pipeline execution will be forced to execute 

52 synchronously, sequentially, and deterministically. 

53 

54 2) Any user-defined functions passed into tf.data transformations such as 

55 `map` will be wrapped in `tf.py_function` so that their body is executed 

56 "eagerly" as a Python function as opposed to a traced TensorFlow graph, which 

57 is the default behavior. Note that even when debug mode is enabled, the 

58 user-defined function is still traced to infer the shape and type of its 

59 outputs; as a consequence, any `print` statements or breakpoints will be 

60 triggered once during the tracing before the actual execution of the input 

61 pipeline. 

62 

63 NOTE: As the debug mode setting affects the construction of the tf.data input 

64 pipeline, it should be enabled before any tf.data definitions. 

65 

66 Raises: 

67 ValueError: When invoked from graph mode. 

68 """ 

69 if context.executing_eagerly(): 

70 toggle_debug_mode(True) 

71 else: 

72 raise ValueError("`enable_debug_mode() is only supported in eager mode.") 

73 

74 

75def toggle_debug_mode(debug_mode): 

76 global DEBUG_MODE 

77 DEBUG_MODE = debug_mode