Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/autograph/core/ag_ctx.py: 66%
44 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2019 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"""Thread-local context managers for AutoGraph."""
17import enum
18import inspect
19import threading
21from tensorflow.python.autograph.utils import ag_logging
22from tensorflow.python.util.tf_export import tf_export
25stacks = threading.local()
28def _control_ctx():
29 if not hasattr(stacks, 'control_status'):
30 stacks.control_status = [_default_control_status_ctx()]
31 return stacks.control_status
34@tf_export('__internal__.autograph.control_status_ctx', v1=[])
35def control_status_ctx():
36 """Returns the current control context for autograph.
38 This method is useful when calling `tf.__internal__.autograph.tf_convert`,
39 The context will be used by tf_convert to determine whether it should convert
40 the input function. See the sample usage like below:
42 ```
43 def foo(func):
44 return tf.__internal__.autograph.tf_convert(
45 input_fn, ctx=tf.__internal__.autograph.control_status_ctx())()
46 ```
48 Returns:
49 The current control context of autograph.
50 """
51 ret = _control_ctx()[-1]
52 return ret
55class Status(enum.Enum):
56 UNSPECIFIED = 0
57 ENABLED = 1
58 DISABLED = 2
61class ControlStatusCtx(object):
62 """A context that tracks whether autograph is enabled by the user."""
64 def __init__(self, status, options=None):
65 self.status = status
66 self.options = options
68 def __enter__(self):
69 _control_ctx().append(self)
70 return self
72 def __repr__(self):
73 return '{}[status={}, options={}]'.format(
74 self.__class__.__name__, self.status, self.options)
76 def __exit__(self, unused_type, unused_value, unused_traceback):
77 assert _control_ctx()[-1] is self
78 _control_ctx().pop()
81class NullCtx(object):
82 """Helper substitute for contextlib.nullcontext."""
84 def __enter__(self):
85 pass
87 def __exit__(self, unused_type, unused_value, unused_traceback):
88 pass
91def _default_control_status_ctx():
92 return ControlStatusCtx(status=Status.UNSPECIFIED)
95INSPECT_SOURCE_SUPPORTED = True
96try:
97 inspect.getsource(ag_logging.log)
98except OSError:
99 INSPECT_SOURCE_SUPPORTED = False
100 ag_logging.warning(
101 'AutoGraph is not available in this environment: functions lack code'
102 ' information. This is typical of some environments like the interactive'
103 ' Python shell. See'
104 ' https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/limitations.md#access-to-source-code'
105 ' for more information.')