• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 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"""Global registry for OpDefs."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
22import threading
23
24from tensorflow.core.framework import op_def_pb2
25from tensorflow.python import _op_def_registry
26
27# The cache amortizes ProtoBuf serialization/deserialization overhead
28# on the language boundary. If an OpDef has been looked up, its Python
29# representation is cached.
30_cache = {}
31_cache_lock = threading.Lock()
32
33
34def get(name):
35  """Returns an OpDef for a given `name` or None if the lookup fails."""
36  try:
37    return _cache[name]
38  except KeyError:
39    pass
40
41  with _cache_lock:
42    try:
43      # Return if another thread has already populated the cache.
44      return _cache[name]
45    except KeyError:
46      pass
47
48    serialized_op_def = _op_def_registry.get(name)
49    if serialized_op_def is None:
50      return None
51
52    op_def = op_def_pb2.OpDef()
53    op_def.ParseFromString(serialized_op_def)
54    _cache[name] = op_def
55    return op_def
56
57
58# TODO(b/141354889): Remove once there are no callers.
59def sync():
60  """No-op. Used to synchronize the contents of the Python registry with C++."""
61