aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/pywrap_dlopen_global_flags.py
diff options
context:
space:
mode:
authorGravatar Allen Lavoie <allenl@google.com>2017-09-21 11:29:45 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-09-21 11:33:40 -0700
commit5c7f9e316d8c7735308a217310350d416d7498cc (patch)
treea3457b97367fe6b1ed4ad47a4284cac88495ae16 /tensorflow/python/pywrap_dlopen_global_flags.py
parent054b88233bf6d6bc5b953fca50dbb01d108b2d18 (diff)
Remove RTLD_GLOBAL when loading pywrap_tensorflow
Splits out a shared object (//tensorflow/libtensorflow_framework.so) with core TensorFlow functionality but neither ops nor kernels. This object does include registries for ops, kernels, filesystems, etc. The expectation is that shared objects containing custom ops will have a runtime dependency on this framework shared object: TensorFlow will load the custom op shared object, and the custom op shared object will use the symbols from the framework shared object to register its ops/kernels/etc. rather than (as before this change) relying on those symbols being in the global symbol table. In this mode, TensorFlow artifacts (_pywrap_tensorflow.so for Python, libtensorflow.so for the C API; currently excluding Android artifacts) will depend on the framework shared object, which will be packaged with the Python pip package and other language distributions. This means that custom ops targeting the framework shared object will work in any language (C++, Java, Go; previously custom ops in these languages required custom Bazel builds). Adds a config option which reproduces the old behavior (--config=monolithic), which for Python means building a monolithic pywrap_tensorflow shared object and loading its symbols into the global symbol table (with RTLD_GLOBAL). As before, there will be no extra-Bazel custom op support for other languages when compiling in this mode. Does not change behavior on Windows; the cmake build is still monolithic. Requires using tf_cc_binary, tf_cc_test, and (rarely) tf_cc_shared_object rules to link in the framework shared object when adding new TensorFlow build rules. PiperOrigin-RevId: 169572746
Diffstat (limited to 'tensorflow/python/pywrap_dlopen_global_flags.py')
-rw-r--r--tensorflow/python/pywrap_dlopen_global_flags.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/tensorflow/python/pywrap_dlopen_global_flags.py b/tensorflow/python/pywrap_dlopen_global_flags.py
new file mode 100644
index 0000000000..509fc2170c
--- /dev/null
+++ b/tensorflow/python/pywrap_dlopen_global_flags.py
@@ -0,0 +1,51 @@
+# Copyright 2017 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.
+# =============================================================================
+"""If possible, exports all symbols with RTLD_GLOBAL.
+
+Note that this file is only imported by pywrap_tensorflow.py if this is a static
+build (meaning there is no explicit framework cc_binary shared object dependency
+of _pywrap_tensorflow_internal.so). For regular (non-static) builds, RTLD_GLOBAL
+is not necessary, since the dynamic dependencies of custom/contrib ops are
+explicit.
+"""
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+import ctypes
+import sys
+
+# On UNIX-based platforms, pywrap_tensorflow is a SWIG-generated
+# python library that dynamically loads _pywrap_tensorflow.so. The
+# default mode for loading keeps all the symbol private and not
+# visible to other libraries that may be loaded. Setting the mode to
+# RTLD_GLOBAL to make the symbols visible, so that custom op libraries
+# imported using `tf.load_op_library()` can access symbols defined in
+# _pywrap_tensorflow.so.
+_use_rtld_global = (hasattr(sys, 'getdlopenflags')
+ and hasattr(sys, 'setdlopenflags'))
+if _use_rtld_global:
+ _default_dlopen_flags = sys.getdlopenflags()
+
+
+def set_dlopen_flags():
+ if _use_rtld_global:
+ sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL)
+
+
+def reset_dlopen_flags():
+ if _use_rtld_global:
+ sys.setdlopenflags(_default_dlopen_flags)