Repository navigation
Expand file tree
/
Copy pathop_def_registry.py
More file actions
60 lines (48 loc) · 1.8 KB
/
Copy pathop_def_registry.py
File metadata and controls
60 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Copyright 2015 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.
# ==============================================================================
"""Global registry for OpDefs."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import threading
from tensorflow.core.framework import op_def_pb2
from tensorflow.python import _op_def_registry
# The cache amortizes ProtoBuf serialization/deserialization overhead
# on the language boundary. If an OpDef has been looked up, its Python
# representation is cached.
_cache = {}
_cache_lock = threading.Lock()
def get(name):
"""Returns an OpDef for a given `name` or None if the lookup fails."""
try:
return _cache[name]
except KeyError:
pass
with _cache_lock:
try:
# Return if another thread has already populated the cache.
return _cache[name]
except KeyError:
pass
serialized_op_def = _op_def_registry.get(name)
if serialized_op_def is None:
return None
op_def = op_def_pb2.OpDef()
op_def.ParseFromString(serialized_op_def)
_cache[name] = op_def
return op_def
# TODO(b/141354889): Remove once there are no callers.
def sync():
"""No-op. Used to synchronize the contents of the Python registry with C++."""