Skip to content

Commit 85d018e

Browse files
authored
Use lazy load to avoid loading libtensorflow_io.so until needed. (#1208)
* Use lazy load to avoid loading libtensorflow_io.so until needed. In tensorflow-io we have two shared libraries: - `libtensorflow_io.so` is linked with TF C++ API and does not have forward compatibility - `libtensorflow_io_plugins.so` is linked with TF C API and does have forward compatibility The plan is to eventually have libtensorflow_io.so linked to TF C API only but that will take a long time and requires rewriting C++ kernel (after modular kernel API is available in TF, unknown status now). With both `libtensorflow_io.so` and `libtensorflow_plugins.so` loaded in `import tensorflow_io as tfio`, the tensorflow-io package is not forward compatible even though our file systems implementations are forward compatible already. In the meantime, one way to allow forward compatible for file systems, is to lazy load `libtensorflow_io.so`: - When user tries to run some function like `decode_json` they still encounter errors if they have newer versions of TF. - However, thwn user tries to just use the file system feature (e.g., azfs/http/etc), it works with newer versions of TF with tensorflow-io through `import tensorflow_io as tfio` (not touching any other submodules in `tensorflow_io`). Signed-off-by: Yong Tang <yong.tang.github@outlook.com> * Added tfio.experimental.oss() function to load OSS file system explicitly Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
1 parent b5c9832 commit 85d018e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

tensorflow_io/core/python/api/experimental/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@
2828
from tensorflow_io.core.python.api.experimental import streaming
2929
from tensorflow_io.core.python.api.experimental import filter
3030
from tensorflow_io.core.python.api.experimental import elasticsearch
31+
32+
33+
def oss():
34+
from tensorflow_io.core.python.ops import core_ops as _core_ops
35+
36+
return _core_ops._load()

tensorflow_io/core/python/ops/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ctypes
1919
import sys
2020
import inspect
21+
import types
2122

2223
import tensorflow as tf
2324

@@ -70,8 +71,29 @@ def _load_library(filename, lib="op"):
7071
)
7172

7273

73-
core_ops = _load_library("libtensorflow_io.so")
74+
class LazyLoader(types.ModuleType):
75+
def __init__(self, name, library):
76+
self._mod = None
77+
self._module_name = name
78+
self._library = library
79+
super().__init__(self._module_name)
80+
81+
def _load(self):
82+
if self._mod is None:
83+
self._mod = _load_library(self._library)
84+
return self._mod
85+
86+
def __getattr__(self, attrb):
87+
return getattr(self._load(), attrb)
88+
89+
def __dir__(self):
90+
return dir(self._load())
91+
92+
93+
core_ops = LazyLoader("core_ops", "libtensorflow_io.so")
7494
try:
7595
plugin_ops = _load_library("libtensorflow_io_plugins.so", "fs")
7696
except NotImplementedError as e:
97+
# Note: load libtensorflow_io.so imperatively in case of statically linking
98+
core_ops = _load_library("libtensorflow_io.so")
7799
plugin_ops = _load_library("libtensorflow_io.so", "fs")

0 commit comments

Comments
 (0)