STT-tensorflow/tensorflow/python/saved_model/load_context.py
Ken Franko e6e0d403a0 Add SavedModel.LoadOptions to hub.KerasLayer API to pass to load_v2.
PiperOrigin-RevId: 322425049
Change-Id: I4fd626c4a7e470bcb647a3d76d1ece16fcb63b28
2020-07-21 13:09:44 -07:00

57 lines
1.5 KiB
Python

# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Context for storing options for loading a SavedModel."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import contextlib
import threading
class LoadContext(threading.local):
"""A context for loading a model."""
def __init__(self):
super(LoadContext, self).__init__()
self._load_options = None
def set_load_options(self, load_options):
self._load_options = load_options
def clear_load_options(self):
self._load_options = None
def load_options(self):
return self._load_options
_load_context = LoadContext()
@contextlib.contextmanager
def load_context(load_options):
_load_context.set_load_options(load_options)
try:
yield
finally:
_load_context.clear_load_options()
def get_load_options():
"""Returns whether under a load context."""
return _load_context.load_options()