Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow_addons/utils/resource_loader.py: 88%
42 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Utilities similar to tf.python.platform.resource_loader."""
17from packaging.version import Version
18import os
19import warnings
21import tensorflow as tf
23INCLUSIVE_MIN_TF_VERSION_FOR_ABI_COMPATIBILITY = "2.15.0"
24EXCLUSIVE_MAX_TF_VERSION_FOR_ABI_COMPATIBILITY = "2.16.0"
25abi_warning_already_raised = False
26SKIP_CUSTOM_OPS = False
29def get_project_root():
30 """Returns project root folder."""
31 return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
34def get_path_to_datafile(path, is_so=False):
35 """Get the path to the specified file in the data dependencies.
37 The path is relative to tensorflow_addons/
39 Args:
40 path: a string resource path relative to tensorflow_addons/
41 Returns:
42 The path to the specified data file
43 """
44 root_dir = get_project_root()
45 if is_so:
46 bazel_bin_dir = os.path.join(os.path.dirname(root_dir), "bazel-bin")
47 if os.path.isdir(bazel_bin_dir):
48 root_dir = os.path.join(bazel_bin_dir, "tensorflow_addons")
49 return os.path.join(root_dir, path.replace("/", os.sep))
52class LazySO:
53 def __init__(self, relative_path):
54 self.relative_path = relative_path
55 self._ops = None
57 @property
58 def ops(self):
59 if SKIP_CUSTOM_OPS:
60 import pytest
62 pytest.skip(
63 "Skipping the test because a custom ops "
64 "was being loaded while --skip-custom-ops was set."
65 )
66 if self._ops is None:
67 self.display_warning_if_incompatible()
68 self._ops = tf.load_op_library(
69 get_path_to_datafile(self.relative_path, is_so=True)
70 )
71 return self._ops
73 def display_warning_if_incompatible(self):
74 global abi_warning_already_raised
75 if abi_is_compatible() or abi_warning_already_raised:
76 return
78 warnings.warn(
79 "You are currently using TensorFlow {} and trying to load a custom op ({})."
80 "\n"
81 "TensorFlow Addons has compiled its custom ops against TensorFlow {}, "
82 "and there are no compatibility guarantees between the two versions. "
83 "\n"
84 "This means that you might get segfaults when loading the custom op, "
85 "or other kind of low-level errors.\n If you do, do not file an issue "
86 "on Github. This is a known limitation."
87 "\n\n"
88 "It might help you to fallback to pure Python "
89 "ops by setting environment variable `TF_ADDONS_PY_OPS=1` or using `tfa.options.disable_custom_kernel()` in your code. "
90 "To do that, see "
91 "https://github.com/tensorflow/addons#gpucpu-custom-ops "
92 "\n\n"
93 "You can also change the TensorFlow version installed on your system. "
94 "You would need a TensorFlow version equal to or above {} and strictly "
95 "below {}.\n Note that nightly versions of TensorFlow, "
96 "as well as non-pip TensorFlow like `conda install tensorflow` or compiled "
97 "from source are not supported."
98 "\n\n"
99 "The last solution is to find the TensorFlow Addons version that has "
100 "custom ops compatible with the TensorFlow installed on your "
101 "system. To do that, refer to the readme: "
102 "https://github.com/tensorflow/addons"
103 "".format(
104 tf.__version__,
105 self.relative_path,
106 INCLUSIVE_MIN_TF_VERSION_FOR_ABI_COMPATIBILITY,
107 INCLUSIVE_MIN_TF_VERSION_FOR_ABI_COMPATIBILITY,
108 EXCLUSIVE_MAX_TF_VERSION_FOR_ABI_COMPATIBILITY,
109 ),
110 UserWarning,
111 )
112 abi_warning_already_raised = True
115def abi_is_compatible():
116 if "dev" in tf.__version__:
117 # tf-nightly
118 return False
120 min_version = Version(INCLUSIVE_MIN_TF_VERSION_FOR_ABI_COMPATIBILITY)
121 max_version = Version(EXCLUSIVE_MAX_TF_VERSION_FOR_ABI_COMPATIBILITY)
122 return min_version <= Version(tf.__version__) < max_version