Skip to content

Commit f095c20

Browse files
Add a warning when trying to load a custom op with a uncompatible version of TF. (#1482)
* Fixing the gpu build. * Add a warning when trying to load a custom op. * Add also the nightly version. * Better formatting. * Update message. * Added message about non-pip TF.
1 parent dd28b69 commit f095c20

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tensorflow_addons/utils/resource_loader.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
# ==============================================================================
1515
"""Utilities similar to tf.python.platform.resource_loader."""
1616

17+
from distutils.version import LooseVersion
1718
import os
19+
import warnings
1820

1921
import tensorflow as tf
2022

23+
MIN_TF_VERSION_FOR_ABI_COMPATIBILITY = "2.1.0"
24+
MAX_TF_VERSION_FOR_ABI_COMPATIBILITY = "2.2.0"
25+
abi_warning_already_raised = False
26+
2127

2228
def get_project_root():
2329
"""Returns project root folder."""
@@ -46,5 +52,56 @@ def __init__(self, relative_path):
4652
@property
4753
def ops(self):
4854
if self._ops is None:
55+
self.display_warning_if_incompatible()
4956
self._ops = tf.load_op_library(get_path_to_datafile(self.relative_path))
5057
return self._ops
58+
59+
def display_warning_if_incompatible(self):
60+
global abi_warning_already_raised
61+
if abi_is_compatible() or abi_warning_already_raised:
62+
return
63+
64+
warnings.warn(
65+
"You are currently using TensorFlow {} and trying to load a custom op ({})."
66+
"\n"
67+
"TensorFlow Addons has compiled its custom ops against TensorFlow {}, "
68+
"and there are no compatibility guarantees between the two versions. "
69+
"\n"
70+
"This means that you might get segfaults when loading the custom op, "
71+
"or other kind of low-level errors.\n If you do, do not file an issue "
72+
"on Github. This is a known limitation."
73+
"\n\n"
74+
"It might help you to fallback to pure Python "
75+
"ops with TF_ADDONS_PY_OPS . To do that, see "
76+
"https://github.com/tensorflow/addons#gpucpu-custom-ops "
77+
"\n\n"
78+
"You can also change the TensorFlow version installed on your system. "
79+
"You would need a TensorFlow version equal to or above {} and strictly "
80+
"below {}.\n Note that nightly versions of TensorFlow, "
81+
"as well as non-pip TensorFlow like `conda install tensorflow` or compiled "
82+
"from source are not supported."
83+
"\n\n"
84+
"The last solution is to find the TensorFlow Addons version that has "
85+
"custom ops compatible with the TensorFlow installed on your "
86+
"system. To do that, refer to the readme: "
87+
"https://github.com/tensorflow/addons"
88+
"".format(
89+
tf.__version__,
90+
self.relative_path,
91+
MIN_TF_VERSION_FOR_ABI_COMPATIBILITY,
92+
MIN_TF_VERSION_FOR_ABI_COMPATIBILITY,
93+
MAX_TF_VERSION_FOR_ABI_COMPATIBILITY,
94+
),
95+
UserWarning,
96+
)
97+
abi_warning_already_raised = True
98+
99+
100+
def abi_is_compatible():
101+
if "dev" in tf.__version__:
102+
# tf-nightly
103+
return False
104+
105+
min_version = LooseVersion(MIN_TF_VERSION_FOR_ABI_COMPATIBILITY)
106+
max_version = LooseVersion(MAX_TF_VERSION_FOR_ABI_COMPATIBILITY)
107+
return min_version <= LooseVersion(tf.__version__) < max_version

0 commit comments

Comments
 (0)