1# Copyright 2020 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
16"""Interface that provides access to Keras dependencies.
17
18This library is a common interface that contains Keras functions needed by
19TensorFlow and TensorFlow Lite and is required as per the dependency inversion
20principle (https://en.wikipedia.org/wiki/Dependency_inversion_principle). As per
21this principle, high-level modules (eg: TensorFlow and TensorFlow Lite) should
22not depend on low-level modules (eg: Keras) and instead both should depend on a
23common interface such as this file.
24"""
25
26
27from __future__ import absolute_import
28from __future__ import division
29from __future__ import print_function
30
31
32_KERAS_CALL_CONTEXT_FUNCTION = None
33_KERAS_CLEAR_SESSION_FUNCTION = None
34_KERAS_GET_SESSION_FUNCTION = None
35_KERAS_LOAD_MODEL_FUNCTION = None
36
37# TODO(scottzhu): Disable duplicated inject once keras is moved to
38# third_party/py/keras.
39# TODO(b/169898786): Use the Keras public API when TFLite moves out of TF
40
41
42# Register functions
43def register_call_context_function(func):
44  global _KERAS_CALL_CONTEXT_FUNCTION
45  _KERAS_CALL_CONTEXT_FUNCTION = func
46
47
48def register_clear_session_function(func):
49  global _KERAS_CLEAR_SESSION_FUNCTION
50  _KERAS_CLEAR_SESSION_FUNCTION = func
51
52
53def register_get_session_function(func):
54  global _KERAS_GET_SESSION_FUNCTION
55  _KERAS_GET_SESSION_FUNCTION = func
56
57
58def register_load_model_function(func):
59  global _KERAS_LOAD_MODEL_FUNCTION
60  _KERAS_LOAD_MODEL_FUNCTION = func
61
62
63# Get functions
64def get_call_context_function():
65  global _KERAS_CALL_CONTEXT_FUNCTION
66  return _KERAS_CALL_CONTEXT_FUNCTION
67
68
69def get_clear_session_function():
70  global _KERAS_CLEAR_SESSION_FUNCTION
71  return _KERAS_CLEAR_SESSION_FUNCTION
72
73
74def get_get_session_function():
75  global _KERAS_GET_SESSION_FUNCTION
76  return _KERAS_GET_SESSION_FUNCTION
77
78
79def get_load_model_function():
80  global _KERAS_LOAD_MODEL_FUNCTION
81  return _KERAS_LOAD_MODEL_FUNCTION
82