xref: /aosp_15_r20/external/tensorflow/tensorflow/python/eager/core.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Copyright 2017 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"""Experimental API for TensorFlow's "Eager" mode of execution."""
16
17from tensorflow.python import pywrap_tfe
18from tensorflow.python.framework import errors
19
20# Trace of execution and memory usage.
21_active_trace = None
22
23
24def _status_to_exception(status):
25  try:
26    error_class = errors.exception_type_from_error_code(status.code)
27    return error_class(None, None, status.message, status.payloads)
28  except KeyError:
29    return errors.UnknownError(None, None, status.message, status.code,
30                               status.payloads)
31
32
33class _NotOkStatusException(Exception):
34  """Exception class to handle not ok Status."""
35
36  def __init__(self, message, code, payloads):
37    super(_NotOkStatusException, self).__init__()
38    self.message = message
39    self.code = code
40    self.payloads = payloads
41
42  def __str__(self):
43    e = _status_to_exception(self)
44    return "%s: %s" % (e.__class__.__name__, e)
45
46
47pywrap_tfe.TFE_Py_RegisterExceptionClass(_NotOkStatusException)
48
49
50class _FallbackException(Exception):
51  """Exception class to handle fallback from the fastpath.
52
53  The fastpath that we refer to here is the one implemented to reduce per-op
54  overheads (TFE_Py_FastPathExecute_C). If the conditions for executing the op
55  on the fastpath are not met, we fallback to a safer (and more complete)
56  slowpath, and this Exception is raised to signal that transition.
57  """
58  pass
59
60
61class _SymbolicException(Exception):
62  """Exception class to handle use of symbolic tensors when executing eagerly.
63
64  `keras.Input()` creates symbolic tensors (in a FuncGraph managed by the
65  Keras backend) while in eager execution. This exception is used to
66  identify this case (raised in `convert_to_tensor` cause generated functions
67  for ops to construct graphs instead of executing the kernel).
68  """
69  pass
70
71
72pywrap_tfe.TFE_Py_RegisterFallbackExceptionClass(_FallbackException)
73