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