-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathload_ops_library.py
72 lines (63 loc) · 2.61 KB
/
load_ops_library.py
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
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (c) 2021-2022 Intel Corporation
#
# 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.
# ==============================================================================
# pylint: disable=missing-module-docstring
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import types
import hashlib
from tensorflow.python.framework import _pywrap_python_op_gen
from tensorflow.python.client import pywrap_tf_session as py_tf
def _load_ops_library():
"""Loads a TensorFlow plugin, containing custom ops and kernels.
Pass "library_filename" to a platform-specific mechanism for dynamically
loading a library. The rules for determining the exact location of the
library are platform-specific and are not documented here. When the
library is loaded, ops and kernels registered in the library via the
`REGISTER_*` macros are made available in the TensorFlow process. Note
that ops with the same name as an existing op are rejected and not
registered with the process.
Args:
library_filename: Path to the plugin.
Relative or absolute filesystem path to a dynamic library file.
Returns:
A python module containing the Python wrappers for Ops defined in
the plugin.
Raises:
RuntimeError: when unable to load the library or get the python wrappers.
"""
buf = py_tf.TF_GetAllOpList()
try:
wrappers = _pywrap_python_op_gen.GetPythonWrappers( # pylint: disable=c-extension-no-member
py_tf.TF_GetBuffer(buf))
finally:
# Delete the buf to release any memory held in C
# that are no longer needed.
py_tf.TF_DeleteBuffer(buf)
# Get a unique name for the module.
module_name = hashlib.sha512(wrappers).hexdigest()
if module_name in sys.modules:
return sys.modules[module_name]
module = types.ModuleType(module_name)
# pylint: disable=exec-used
exec(wrappers, module.__dict__)
# Allow this to be recognized by AutoGraph.
setattr(module, '_IS_TENSORFLOW_PLUGIN', True)
sys.modules[module_name] = module
return module
load_ops_library = _load_ops_library()